如何单独添加或删除每个页面的元数据?

时间:2012-05-28 作者:john cer

如何为特定页面添加元框,例如:仅针对关于我们的页面。

我该怎么办?

当前代码为所有页面添加元框:

function myplugin_add_custom_box() {
    add_meta_box( 
        \'myplugin_sectionid\',
        __( \'My Post Section Title\', \'myplugin_textdomain\' ),
        \'myplugin_inner_custom_box\',
        \'post\' 
    );
    add_meta_box(
        \'myplugin_sectionid\',
        __( \'My Post Section Title\', \'myplugin_textdomain\' ), 
        \'myplugin_inner_custom_box\',
        \'page\'
    );
}

2 个回复
SO网友:Chris_O

这里有一个函数,您可以传递单个post id或id数组。在添加元框的函数中调用此函数。如果id或id不匹配,元框将不会显示在该帖子或页面上。

function check_id( $id ) {
    // Get the current post ID
    if ( isset( $_GET[ \'post\' ] ) ) $post_id = $_GET[ \'post\' ];
    elseif ( isset( $_POST[ \'post_ID\' ] ) ) $post_id = $_POST[ \'post_ID\' ];
        if ( ! isset( $post_id ) )
            return false;
    // If value isn\'t an array, turn it into one
    $id = ! is_array ( $id ) ? array ( $id ) : $id;

    // If current page id is in the included array, display the metabox
    if ( in_array ( $post_id, $id ) )
        return true;
    else
        return false;
}

 function myplugin_add_custom_box() {
    $what_page = check_id( array( 3, 18, 15 ) );
    if ( false == $what_page ) return;
    add_meta_box( 
        \'myplugin_sectionid\',
        __( \'My Post Section Title\', \'myplugin_textdomain\' ),
        \'myplugin_inner_custom_box\',
        \'post\' 
    );
    add_meta_box(
        \'myplugin_sectionid\',
        __( \'My Post Section Title\', \'myplugin_textdomain\' ), 
        \'myplugin_inner_custom_box\',
        \'page\'
    );
}

SO网友:brasofilo

这里还有另一种可能性:根据页面模板或帖子类别显示或隐藏元框。

  • page-wpse-53486.php 是模板文件名
  • form#adv-settings label[for=\'myplugin_sectionid-hide 是屏幕选项中的元框选项。如果元框不可用于帖子/页面,我们不希望它出现
  • #in-category-6 是ID为6的类别的复选框
add_action(\'admin_head\', \'wpse_53486_script_enqueuer\');
function wpse_53486_script_enqueuer() {
    global $current_screen;
    if(\'page\' == $current_screen->id) 
    {
        echo <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {

                /**
                 * Adjust visibility of the meta box at startup
                */
                if($(\'#page_template\').val() == \'page-wpse-53486.php\') {
                    // show the meta box
                    $(\'#myplugin_sectionid\').show();
                    $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").show();
                } else {
                    // hide your meta box
                    $(\'#myplugin_sectionid\').hide();
                    $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").hide();
                }

                // Debug only
                // - outputs the template filename
                // - checking for console existance to avoid js errors in non-compliant browsers
                if (typeof console == "object") 
                    console.log (\'default value = \' + $(\'#page_template\').val());

                /**
                 * Live adjustment of the meta box visibility
                */
                $(\'#page_template\').live(\'change\', function(){
                        if($(this).val() == \'page-wpse-53486.php\') {
                        // show the meta box
                        $(\'#myplugin_sectionid\').show();
                        $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").show();
                    } else {
                        // hide your meta box
                        $(\'#myplugin_sectionid\').hide();
                        $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").hide();
                    }

                    // Debug only
                    if (typeof console == "object") 
                        console.log (\'live change value = \' + $(this).val());
                });                 
            });    
            </script>
HTML;
    } 
    elseif ( \'post\' == $current_screen->id ) 
    {
        echo <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if ( $(\'#in-category-6\').is(\':checked\') ) {
                    $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").show();
                    $(\'#myplugin_sectionid\').show();
                } else {
                    $(\'#myplugin_sectionid\').hide();
                    $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").hide();
                }

                $(\'#in-category-6\').live(\'change\', function(){
                    if ( $(this).is(\':checked\') ) {
                        $(\'#myplugin_sectionid\').show();
                        $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").show();
                    } else {
                        $(\'#myplugin_sectionid\').hide();
                        $("form#adv-settings label[for=\'myplugin_sectionid-hide\']").hide();
                    }
                });                 
            });    
            </script>
HTML;
    }
}

结束

相关推荐

更改默认自定义字段Metabox名称

我发现元框的标题不能通过cctm plugin. 它将“自定义字段”显示为默认值,这很恼人。Image所以我决定改变它的功能。php,下面是我的代码:add_filter(\'add_meta_boxes\', \'change_meta_box_titles\'); function change_meta_box_titles() { $wp_meta_boxes[\'my_post_type\'][\'normal\'][\'core\'][\'cctm_default\'][\'t