如何为特定类别的帖子添加Metabox

时间:2014-07-31 作者:terminator

我想为category=18的帖子添加Metabox,我正在使用以下代码,但无法这样做。所以请帮帮我->

 add_action(\'admin_init\',\'my_meta_init\');

function my_meta_init()
{
    $post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\'] ;

    // checks for post/page ID
    if ($post_id->post_category[0] == 18)
    {
        add_meta_box(\'team_meta\', \'My Custom Meta Box 1\', \'team_meta_1\', \'post\', \'normal\', \'high\');


    function team_meta_1(){
    global $post;
    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="\' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    // Get the location data if its already been entered
    $designation = get_post_meta($post->ID, \'_designation\', true);

    // Echo out the field
    // echo \'<input type="text" name="_description" value="\' . $description  . \'" />\';
    echo \'<textarea name=_designation rows="6" cols="100">\'.$designation.\'</textarea>\';

    }

    function my_meta_save($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[\'eventmeta_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;

    // OK, we\'re authenticated: we need to find and save the data
    // We\'ll put it into an array to make it easier to loop though.

    $events_meta[\'_designation\'] = $_POST[\'_designation\'];


    // Add values of $events_meta as custom fields

    foreach ($events_meta as $key => $value) { // Cycle through the $events_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 (unlikely)
        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
    }

}

 add_action(\'save_post\',\'my_meta_save\');
}


}

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

而不是if ($post_id->post_category[0] == 18) 尝试

if ( $post_id && in_category( 18, $post_id ) )
还有\'save_post\' 操作应为

 add_action(\'save_post\',\'my_meta_save\', 10, 2);
如果您希望在选择类别时,metabox出现在新帖子上,那么请删除外部类别测试If语句,以便始终添加metabox,然后使用jquery显示/隐藏(将其放在文本区域的回显之后)

echo \'<textarea name=_designation rows="6" cols="100">\'.$designation.\'</textarea>\';
?>
<script type="text/javascript">
jQuery(document).ready(function() {
    (function ($) {
        $(\'#in-category-18\').change(function () { $(\'#team_meta\').toggle(this.checked); }).change();
    })(jQuery);
});
</script>
<?php

结束

相关推荐

Subscribers to posts

把每一篇文章都当作对话的主题,并评论一段对话。现在一切都很好。我创建了一个名为“对话”的帖子类型。现在,我想列出用户参与的所有对话。如何扩展wordpress的此功能?我的想法是创建一个名为wp_posts_users 具有user_id 和conv_id. 这方面有什么最佳做法吗?我的最终目标是在用户页面上列出他参与的所有对话。