在特定页面模板上显示Meta Box

时间:2014-02-10 作者:Derek

是否有办法仅在特定页面模板上显示自定义页面设置?

例如,我有一个设置字段:“自定义设置”。我不想在“默认页面模板”上显示此内容。我只想在选择“自定义页面设置页面模板”时显示它。

以下是我的元框代码:

function cd_meta_box_add()  
{  
add_meta_box( \'icon-class-meta-box\', \'Icon Class\', \'ic_meta_box_cb\', \'page\', \'side\', \'default\' );  
}  
add_action( \'add_meta_boxes\', \'cd_meta_box_add\' ); 

function ic_meta_box_cb( $post)  
{  
$values = get_post_meta( $post->ID );   
$selected = isset( $values[\'ic_meta_box_select\'] ) ? esc_attr( $values[\'ic_meta_box_select\'][0] ) : ”;  

wp_nonce_field( \'ic_meta_box_nonce\', \'meta_box_nonce\' ); 
?>  
<p>  
    <label for="ic_meta_box_select">Class</label>  
    <select name="ic_meta_box_select" id="ic_meta_box_select">
        <option value=" " <?php selected( $selected, \' \' ); ?>> </option> 
        <option value="red" <?php selected( $selected, \'red\' ); ?>>red</option>  
        <option value="blue" <?php selected( $selected, \'blue\' ); ?>>blue</option>
        <option value="green" <?php selected( $selected, \'green\' ); ?>>green</option>
        <option value="pink" <?php selected( $selected, \'pink\' ); ?>>pink</option>
        <option value="orange" <?php selected( $selected, \'orange\' ); ?>>orange</option>
        <option value="black" <?php selected( $selected, \'black\' ); ?>>black</option>
    </select>  
</p>
<?php    
}

2 个回复
最合适的回答,由SO网友:Brian Fegter 整理而成

我通常通过挂钩使用CSS和jQuery来实现这类功能admin_head. 这应该在客户端站点完成,因为用户可以在DOM加载后选择您的模板或另一个模板。基本上,您所要做的就是检查该值是否设置为#page_template 和切换show/hide 是否选择了模板。

add_action( \'admin_head-post.php\', \'metabox_switcher\' );
add_action( \'admin_head-post-new.php\', \'metabox_switcher\' );

function metabox_switcher( $post ){

    #Isolate to your specific post type
    if( $post->post_type === \'page\' ){

        #Locate the ID of your metabox with Developer tools
        $metabox_selector_id = \'id-of-your-metabox\';

        echo \'
            <style type="text/css">
                /* Hide your metabox so there is no latency flash of your metabox before being hidden */
                #\'.$metabox_selector_id.\'{display:none;}
            </style>
            <script type="text/javascript">
                jQuery(document).ready(function($){

                    //You can find this in the value of the Page Template dropdown
                    var templateName = \\\'template-folder/file-name.php\\\';

                    //Page template in the publishing options
                    var currentTemplate = $(\\\'#page_template\\\');

                    //Identify your metabox
                    var metabox = $(\\\'#\'.$metabox_selector_id.\'\\\');

                    //On DOM ready, check if your page template is selected
                    if(currentTemplate.val() === templateName){
                        metabox.show();
                    }

                    //Bind a change event to make sure we show or hide the metabox based on user selection of a template
                    currentTemplate.change(function(e){
                        if(currentTemplate.val() === templateName){
                            metabox.show();
                        }
                        else{
                            //You should clear out all metabox values here;
                            metabox.hide();
                        }
                    });
                });
            </script>
        \';
    }
}

SO网友:Bipin
add_action(\'add_meta_boxes\', \'add_product_meta\');
function add_product_meta() {
    global $post;

    if(!empty($post)) {
        $pageTemplate = get_post_meta($post->ID, \'_wp_page_template\', true);

        if($pageTemplate == \'page-templates/product-page.php\' ) {
            add_meta_box(
                \'product_meta\', // $id
                \'Product Information\', // $title
                \'display_product_information\', // $callback
                \'page\', // $page
                \'normal\', // $context
                \'high\'); // $priority
        }
    }
}

function display_product_information() {
    // Add the HTML for the post meta
}
结束

相关推荐

从自定义帖子类型中删除“显示共享按钮”metabox Jetpack

我已经安装了Jetpack,现在我的自定义帖子类型有一个元框,上面写着“共享:显示共享按钮”,还有一个复选框。这对于这种自定义帖子类型是不必要的,我想将其全部删除(而不仅仅是通过屏幕选项隐藏它)。我尝试添加add_action( \'init\', array( $this, \'my_remove_filters_func\' ) ); function my_remove_filters_func() { remove_all_filters( \'the_conte