【问题标题】:How do I display Youtube/Vimeo video ID on custom post type when user enters it in custom meta box?当用户在自定义元框中输入自定义帖子类型时,如何在自定义帖子类型上显示 Youtube/Vimeo 视频 ID?
【发布时间】:2013-09-30 19:54:19
【问题描述】:

我创建了一个 2x 短代码,当用户在其中输入视频 ID 时,它会显示 youtube 视频和 vimeo 视频,例如:[youtube id=""] [vimeo id=""]。

我没有在我的帖子中输入这个,而是尝试创建一个自定义元框,用户可以在其中输入 id 然后显示它。

到目前为止,我已经能够创建一个插件来显示元框,但我不确定如何获得它来保存 youtube/vimeo ID 并显示视频。

我需要 2 个不同的元框吗?一个是用户可以输入youtube视频ID,另一个是输入vimeo ID然后显示在前端?

元框的代码是:

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
}

function cd_meta_box_cb( $post )
{
    $values = get_post_custom( $post->ID );
    $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';

    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <label for="my_meta_box_text">Youtube/Vimeo ID:</label>
        <input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
    </p>


    <?php   
}


add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );

    // Probably a good idea to make sure your data is set
    if( isset( $_POST['my_meta_box_text'] ) )
        update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );


}
?>

我还认为我需要在我的 single-video.php 模板上运行一个循环,以便 wordpress 知道显示来自自定义元框的 ID。对于循环,这就是我到目前为止所拥有的一切:

$args = array( 'post_type' => 'videos', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'ASC' );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); 

虽然这还没有完成,所以我非常感谢任何帮助。

【问题讨论】:

    标签: wordpress youtube vimeo


    【解决方案1】:

    你走的路是正确的。您需要创建元框来选择 video_type 它将 youtube/vimeo。那么您需要提供一个文本框来为相应的视频类型添加 id。 代码如下所示

    <?php 
    add_action( 'add_meta_boxes', 'cd_meta_box_add' );
    function cd_meta_box_add()
    {
     add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
    }
    
    function cd_meta_box_cb( $post )
    {
    $vedio_type = get_post_meta($post->ID,'my_vedio_type',true);
    $vedio_id = get_post_meta($post->ID,'my_meta_box_text',true);
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    
    <p>
        <label for="my_meta_box_text">Select vedio type:</label>
         <!-- added select for selecting vedio type -->
        <select name="my_vedio_type" id="my_vedio_type">  
            <option <?php echo ($vedio_type == 'youtube') ? "selected='selected'" : "" ;?> value="youtube">Youtube</option>
            <option <?php echo ($vedio_type == 'vimeo') ? "selected='selected'" : "" ;?> value="vimeo">Vimeo</option>
        </select>
        <!-- added select for selecting vedio type -->
    </p>
    
    <p>
        <label for="my_meta_box_text">Youtube/Vimeo ID:</label>
        <input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $vedio_id; ?>" />
    </p>
    
    
    <?php   
      }
    
    
     add_action( 'save_post', 'cd_meta_box_save' ); 
     function cd_meta_box_save( $post_id )
    {
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;
    
    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    
    // Probably a good idea to make sure your data is set
    if( isset( $_POST['my_vedio_type'] ) )
    update_post_meta( $post_id, 'my_vedio_type', wp_kses( $_POST['my_vedio_type'], $allowed ) );
    if( isset( $_POST['my_meta_box_text'] ) )
        update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
    
    
     }
     ?>
    

    之后,您可以在自定义帖子类型 single.php 文件中使用以下代码检索这些值

    <?php 
     if(get_post_meta($post->ID,'my_vedio_type',true) == "youtube"){
     $youtube_id = get_post_meta($post->ID,'my_meta_box_text',true);     
      }
      if(get_post_meta($post->ID,'my_vedio_type',true) == "vimeo"){
     $vimeo_id = get_post_meta($post->ID,'my_meta_box_text',true);   
       }
    
    
       ?>
    

    【讨论】:

    • 感谢您的回复,不幸的是,当我将代码放入 single-video.php 时,它根本没有显示任何内容。这是我在 single-video.php 中的代码:code
      ID,'my_video_type',true) == "youtube"){ $youtube_id = get_post_meta($post->ID,'my_meta_box_text',true); } if(get_post_meta($post->ID,'my_video_type',true) == "vimeo"){ $vimeo_id = get_post_meta($post->ID,'my_meta_box_text',true); } ?>
    • 您能否使用这些自定义元检查它是否显示任何虚拟文本,如果没有,可能会有一些问题。例如,您可以检查 $post->ID 是否返回 id。
    • 我需要把它放在single-video.php 中吗?我添加了它,但似乎没有什么不同。有什么理由不显示任何内容吗?我的代码如下:code &lt;?php global $post; ?&gt; &lt;?php if(get_post_meta($post-&gt;ID,'my_video_type',true) == "youtube"){ $youtube_id = get_post_meta($post-&gt;ID,'my_meta_box_text',true); } if(get_post_meta($post-&gt;ID,'my_video_type',true) == "vimeo"){ $vimeo_id = get_post_meta($post-&gt;ID,'my_meta_box_text',true); } ?&gt;
    • 在后端我可以看到自定义元框、下拉列表和视频 ID 字段,由于某种原因,前端没有显示任何内容。当我使用 echo the_content();它显示帖子中的内容,但不显示放入元框的内容。
    • 对不起@brasofilo 我在哪里添加?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多