显示自定义帖子类型的自定义字段中的信息

时间:2013-09-27 作者:Neelam Khan

我将视频设置为自定义帖子类型,并将音乐、商业、促销设置为此帖子类型中的类别:

我有一个功能,可以在wordpress后端的视频帖子类型页面上显示自定义元框。用户可以输入youtube视频的ID或vimeo视频的ID-wordpress然后在自定义帖子类型页面上显示ID的视频。当用户将新帖子添加到视频自定义帖子类型并将其分配到我指定的任何类别时,我希望wordpress显示不同的视频。我现在的代码并没有达到我想要的效果,因为它在每篇文章上显示相同的视频,即使其中一些文章上没有指定ID。例如,在music post页面上,我为其分配了类别music,并在前端显示了一个vimeo视频ID,但在促销和商业中显示了相同的视频,我不希望发生这种情况。我运行的循环是(在single videos.php中):

<?php

$args = array( \'post_type\' => \'videos\', \'posts_per_page\' => 20, \'orderby\' => \'date\', \'order\' => \'ASC\' );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
//$args = array( \'post_type\' => \'videos\', \'posts_per_page\' => 20, \'orderby\' => \'date\', \'order\' => \'ASC\' );
$ytubeID = get_post_meta($post->ID, \'_youtubeID\', true);
$vimID = get_post_meta($post->ID, \'_vimeoID\', true);
if ($ytubeID || $vimID){
if ($ytubeID){

echo \'<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/\'.$ytubeID.\'"  allowfullscreen="true" frameborder="0" width="640" height="390">\';

echo \'</iframe>\';
} elseif ($vimID){
echo \'<br />\';
echo \'<iframe src="http://player.vimeo.com/video/\'.$vimID.\'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>\';
}//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
}

endwhile;
                    wp_reset_query();

?>
下面是创建元框的完整函数&;存储信息:

// Create the Video Information Meta Box by hooking into the admin menu for a post
    add_action(\'admin_menu\', \'video_add_box\');

    //Function call by the "add_action" to tell it what to add, "video_information" and where "post"
    function video_add_box(){
    add_meta_box(\'video_information\', \'Video Information\', \'video_information\', \'videos\', \'normal\', \'high\');
    }

    //function to populate the meta box added above
    function video_information(){
    global $post;

    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="video_noncename" id="video_noncename" value="\' .
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    //adds the custom field _youtubeID plus some other stuff
    $youtubeID = get_post_meta($post->ID, \'_youtubeID\', true);
    if ( empty($youtubeID) ) {
    $youtubeID = \'\';
    }

    //adds the custom field _vimeoID
    $vimeoID = get_post_meta($post->ID, \'_vimeoID\', true);
    if ( empty($vimeoID) ) {
    $vimeoID = \'\';
    }

    //add the box
    echo \'<br />\';
    echo \'<strong>Youtube ID:</strong>  <input type="text" name="_youtubeID" value="\' . $youtubeID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    echo \'<strong>Vimeo ID:</strong>  <input type="text" name="_vimeoID" value="\' . $vimeoID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    } //end video_information function

    //save_video_meta is called below with the action "save_post" and saves your IDs to the post
    function save_video_meta($post_id, $post) {
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !wp_verify_nonce( $_POST[\'video_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?

    if ( !current_user_can( \'edit_post\', $post->ID )){
    return $post->ID;
    }

    $video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
    $video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
    foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
    if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice

    $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV

    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
    update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn\'t have a value
    add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }//endforeach video meta

    } //end save_video_meta

    //save the video custom fields
    add_action(\'save_post\', \'save_video_meta\', 1, 2);

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

我认为您将错误的post ID值传递给get_post_meta() 作用尝试以下操作:

$ytubeID = get_post_meta(get_the_ID(), \'_youtubeID\', true);
$vimID = get_post_meta(get_the_ID(), \'_vimeoID\', true);
Note:, 由于您正在使用WP\\u Query的新实例,因此不需要wp_reset_query();, 使用wp_reset_postdata(); 相反

在添加用于存储自定义字段值的完整代码之前,我只能说,您再次向update_post_meta()delete_post_meta() 函数。。通常,post ID会传递给save函数回调,而$post->ID是错误的(您正在进行的$post->post\\u类型检查也是错误的)。您还将向save回调传递第二个参数,但save回调只接受一个参数。尝试以下操作:

add_action( \'save_post\', \'my_save_postdata\' );
function my_save_postdata($post_id){
    $video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
    $video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
    foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
        if(  $_POST[\'post_type\'] == \'revision\' ) return; // Don\'t store custom data twice
        $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV

        if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post_id, $key, $value);
        } else { // If the custom field doesn\'t have a value
            add_post_meta($post_id, $key, $value);
        }
        if(!$value) delete_post_meta($post_id, $key); // Delete if blank
    }//endforeach video meta
 }
您可以进行进一步的改进,但它们超出了答案的范围,我举个例子,以下代码:

        if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post_id, $key, $value);
        } else { // If the custom field doesn\'t have a value
            add_post_meta($post_id, $key, $value);
        }
        if(!$value) delete_post_meta($post_id, $key); // Delete if blank
可以是:

if($value) {
    update_post_meta($post_id, $key, $value);
} else {
    delete_post_meta($post_id, $key); // Delete if blank
}
因为update_post_meta() 将检查自定义字段是否已经有值并将其更新,如果自定义字段没有值(键不存在),则函数将调用add_post_meta().

代码中的所有上述内容:

// Create the Video Information Meta Box by hooking into the admin menu for a post
// Add the meta boxes in the add_meta_boxes action hook, no in admin menu
    add_action(\'add_meta_boxes\', \'video_add_box\');

    //Function call by the "add_action" to tell it what to add, "video_information" and where "post"
    function video_add_box(){
    add_meta_box(\'video_information\', \'Video Information\', \'video_information\', \'videos\', \'normal\', \'high\');
    }

    //function to populate the meta box added above
    function video_information(){
    global $post;

    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="video_noncename" id="video_noncename" value="\' .
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    //adds the custom field _youtubeID plus some other stuff
    $youtubeID = get_post_meta($post->ID, \'_youtubeID\', true);
    if ( empty($youtubeID) ) {
    $youtubeID = \'\';
    }

    //adds the custom field _vimeoID
    $vimeoID = get_post_meta($post->ID, \'_vimeoID\', true);
    if ( empty($vimeoID) ) {
    $vimeoID = \'\';
    }

    //add the box
    echo \'<br />\';
    echo \'<strong>Youtube ID:</strong>  <input type="text" name="_youtubeID" value="\' . $youtubeID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    echo \'<strong>Vimeo ID:</strong>  <input type="text" name="_vimeoID" value="\' . $vimeoID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    } //end video_information function

    //save_video_meta is called below with the action "save_post" and saves your IDs to the post
    function save_video_meta($post_id) {
    // the $post parameter you wass passing has any effect, the save
    // action only accept one parameter. If you need all the post data beeing saved
    // you can access to the global $post_data; or to the superglobal $_POST

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !wp_verify_nonce( $_POST[\'video_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?

    if ( !current_user_can( \'edit_post\', $post->ID )){
    return $post->ID;
    }

    $video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
    $video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
    foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
    if( $_POST[\'post_type\'] == \'revision\' ) return; // Don\'t store custom data twice

    $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV

        if($value) {
            update_post_meta($post_id, $key, $value);
        } else {
            delete_post_meta($post_id, $key); // Delete if blank
        } 
    }//endforeach video meta

    } //end save_video_meta

    //save the video custom fields
    add_action(\'save_post\', \'save_video_meta\', 1, 2);
Just a reminder: Wordpress可以处理大量的外部视频提供商,如youtube和vimeo。只需将视频URL粘贴到编辑器框中,它就会自动显示在前端。此外,还有一个内置的分类法“post format”,可以让您通过“视频格式”过滤帖子。简言之,我认为完全不需要用自定义元框注册一个新的帖子类型,只需显示“视频类型”的帖子;使用内置特性,只需很少的额外代码即可实现相同的功能。

结束

相关推荐

JQuery multiple toggle values

我正试图隐藏/显示依赖于所选页面模板的元数据库。直到我在多个选择的页面模板上显示元盒之前,这一直都很好。下面的代码显示默认页面的富内容编辑器,但不显示页面子级。php页面。我猜我的代码写错了:(function($){ $(function() { $(\'#page_template\').change(function() { $(\'#postdivrich\').toggle($(this).val() == \'page-c