1

I'm having issues getting some custom meta boxes to display on the page default template. I want these meta boxes to only display on the default template and disappear when the user chooses a different page template. Below is the code I have tried.

add_action( 'add_meta_boxes', 'add_page_meta' );

function add_page_meta() {

    global $post;

    if(!empty($post)) {

        $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);

        if( $pageTemplate == 'page-templates/page.php') {
            add_meta_box('banner-attributes', 'Banner Attributes', 'banner_attributes_callback', 'page', 'side');
            echo("<script>console.log('true');</script>");
        }
    }
}

I also noticed when changing the if condition from 'page-templates/page.php' to 'page-templates/page-notitle.php' the function would work as intended. I have both a page.php and a page-notitle.php within my page-templates folder. Do I need to reference the default template as something other than page.php?

Thanks!

1 Answer 1

0

Maybe your problem comes from the fact that page.php is a reserved template name for default template file which should be located in the root directory of your theme and not in a sub dir such as page-templates/page.php.

You can check the template hierarchy schema here : https://codex.wordpress.org/images/c/ca/Template_Hierarchy_2015.png

1
  • Thanks, I believe you're right. I moved my page.php template file back into my root directory and this solved my issue! Mar 26, 2018 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.