如何在保存帖子前和保存帖子后为特定类别的帖子添加Metabox

时间:2015-01-18 作者:Jordan Leigh

我想为category=5的帖子添加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] == 5)
    if ( $post_id && in_category( 5, $post_id ) )
    {
        add_meta_box(\'team_meta\', \'My Custom Meta Box 1\', \'team_meta\', \'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 \'<textarea name=_designation rows="6" cols="100">\'.$designation.\'</textarea>\';
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function() {
        (function ($) {
            $(\'#in-category-5\').change(function () {alert()}).change();
        })(jQuery);
    });
    </script>
    <?php

    }

    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\', 10, 2);
}


}

1 个回复
SO网友:Erica

首先,请记住,因为这是基于类别ID的,所以在出现框之前,需要将帖子保存在类别“5”下。当我测试你的代码时,在我保存帖子后,meta框确实出现了,但你有一个错误。

add_meta_box(\'team_meta\', \'My Custom Meta Box 1\', \'team_meta\', \'post\', \'normal\', \'high\');
这一行实际上应该是这样的,因为您的函数标题有点不同:

add_meta_box(\'team_meta\', \'My Custom Meta Box 1\', \'team_meta_1\', \'post\', \'normal\', \'high\');
第三个参数是回调函数,您将其命名为“team\\u meta\\u 1”。更改后,文本区域出现并为我正确保存。

结束

相关推荐

Re-order posts in query after

对于特定类别,我需要合并到另一个自定义帖子类型中。现在,我想基于自定义字段进行查询。这可能吗?下面是我合并查询的方式。if (in_category( \'Events\' ) && is_archive()) { global $wp_query; $args = array ( \'post_type\' => \'custom_post_type\' ); $second_query = n