如果选择了视频,则更新数据库值? [英] Update a database value if a video is selected?
问题描述
我将尝试解释这一点,我有一个按钮,我可以在其中上传和选择要添加到帖子中的视频.我有一个输入字段,我可以输入一个持续时间(以秒为单位).我做了一个从数据库中提取视频长度的函数,这个长度我想放在slide_duration
下的数据库中.如果没有选择视频,我希望它像往常一样工作,您可以输入自定义值(以秒为单位),也可以将此输入留空(使其恢复为标准值).
I will try to explain this, I have a button where I can upload and select a video to add to a post. And I have a input field which I can put in a duration (in seconds). I have made a function which pulls the length of the video from the database, and this length I want to put in the database under slide_duration
. And if a video isn't selected I want it to work as it usually do, you can put in a custom value (in seconds) or you can leave this input empty (which makes it revert to the standard value).
相关的 PHP 如下所示:
The related PHP looks like this:
if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration'] >= 1 ) {
$slide_duration = intval($_POST['slide_duration']);
update_post_meta( $post_id, 'slide_duration', $slide_duration );
}
else {
delete_post_meta( $post_id, 'slide_duration' );
}
这是我触摸之前的样子.如果没有输入值,它会删除 slide_duration
,这就是我需要保留它的方式,如果没有任何内容,它会恢复到默认值.我现在在上面的代码之后添加了这部分:
This is how it looked before I touched it. It deletes slide_duration
if no value is entered, this is how I need to keep it, to make it revert to the default value if nothing is put there.
I now added this part after the above code:
if ( isset( $_POST['background-video'] ) ) {
update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
}
这将在选择视频时更新数据库中的 slide_duration
.但是我无法在输入字段中手动设置时间,它在保存/更新帖子时总是恢复为 0
.如果我将代码放在原始代码之上,我可以在输入中输入任何我想要的值,但选择视频将不再更新 slide_duration
.
This will update the slide_duration
in the database when a video is selected. But I cannot manually set the time in the input field, it always reverts to 0
when saving/updating the post. If I put the code above the original, I can put in whatever value I want in the input, but selecting a video won't update slide_duration
any longer.
有关更多上下文,这是输入的外观:
For more context, this is how the input looks:
<?php
$slide_duration = isset( $theme_stored_meta['slide_duration'] ) ? intval($theme_stored_meta['slide_duration'][0]) : '';
?>
<input type="number" name="slide_duration" value="<?=$slide_duration?>" min="1" step="1" style="width: 70px;" /> <?=__('seconds', 'theme')?>
希望我已经解释得足够好并提供了足够的代码来清楚地了解这一点.
Hope I have explained well enough and provided enough of the code to get a clear picture of this.
也试过这个:
if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration'] >= 1 ) {
$slide_duration = intval($_POST['slide_duration']);
update_post_meta( $post_id, 'slide_duration', $slide_duration );
}
elseif ( isset( $_POST['slide_duration'] ) ) {
update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
}
if ( is_null( $_POST['slide_duration'] ) ) {
delete_post_meta( $post_id, 'slide_duration' );
}
推荐答案
终于搞定了!
我真的不知道它做了什么,但这是有效的:
I don't really know what did it, but this is working:
if ( $_POST['slide_duration'] == NULL ) {
delete_post_meta( $post_id, 'slide_duration' );
}
if ( isset( $_POST['slide_duration'] ) && is_numeric( $_POST['slide_duration'] ) && $_POST['slide_duration'] >= 1 ) {
update_post_meta( $post_id, 'slide_duration', (int) $_POST['slide_duration'] );
}
if( isset( $_POST[ 'background-video' ] ) && $video_meta['length'] >= 1 ) {
update_post_meta( $post_id, 'slide_duration', $video_meta['length'] );
}
非常感谢莎莉 CJ
这篇关于如果选择了视频,则更新数据库值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!