$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - How to show metabox on page if it is using a template|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme development - How to show metabox on page if it is using a template

matteradmin10PV0评论

I was creating metaboxes for a special kind of pages. But i got stuck when showing metaboxes to the pages only who currently have the template 'Sky'. my template file name is sky-template.php and holds this code.

/*
Template Name: Sky
*/

And I am adding metaboxes using this function.

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {
    add_meta_box( 'sky_page_excerpt', 'SkyScraper Page Excerpt and Links', 'sky_page_excerpts', 'page', 'advanced', 'high' );
}

I want to add the metaboxes only to pages with template sky. How will i do that. Thanks in advance

I was creating metaboxes for a special kind of pages. But i got stuck when showing metaboxes to the pages only who currently have the template 'Sky'. my template file name is sky-template.php and holds this code.

/*
Template Name: Sky
*/

And I am adding metaboxes using this function.

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {
    add_meta_box( 'sky_page_excerpt', 'SkyScraper Page Excerpt and Links', 'sky_page_excerpts', 'page', 'advanced', 'high' );
}

I want to add the metaboxes only to pages with template sky. How will i do that. Thanks in advance

Share Improve this question asked Mar 13, 2019 at 10:01 Raashid DinRaashid Din 2182 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use the page-specific meta box hook & accept the $post object that's passed to it - then you can check if it currently has a page template of sky-template.php.

add_action( 'add_meta_boxes_page', function ( $post ) {
    if ( $post->_wp_page_template === 'sky-template.php' ) {
        add_meta_box( 'sky_page_excerpt', 'SkyScraper Page Excerpt and Links', 'sky_page_excerpts', 'page', 'advanced', 'high' );
    }
});

Note: this will only work on submit/refresh i.e. when you first (de)select the template when editing, you'll need to save changes and have the page reload for the meta box to take affect.

Post a comment

comment list (0)

  1. No comments so far