5

I want to add add_meta_box to specific page type like Page Template, Like Product Template.

I am using this article http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/ to try it.

7

If your custom page template filename is foobar.php, you can use get_post_meta():

global $post;
if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
    // The current page has the foobar template assigned
    // do something
}

Personally, I like calling this inside my add_meta_boxes_page callback, and wrapping it around the add_meta_box() call itself.

function wpse82477_add_meta_boxes_page() {
    global $post;
    if ( 'foobar.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
        add_meta_box( $args );
    }
}
add_action( 'add_meta_boxes_page', 'wpse82477_add_meta_boxes_page' );

You'll just need to instruct users to save the page after assigning the template, so that the meta box appears.

  • Thanks a lot, one prob here to inform user to save page once. BTW its saved ma lot time. Thanks CHIP – Ajay Patel Jan 21 '13 at 13:25
  • Thanks for the solution. Couldn't remember if add_meta_boxes was too early for this. Might suggest using if( 'foobar.php' == get_page_template_slug( $post->ID )) over using the meta-key directly. – Jer Jul 25 '17 at 16:45
2

I found a few bugs with the answer above option above but I customized this option below and it helped - no bugs at all.

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
}

https://paulund.co.uk/display-post-meta-box-specific-page-templates

It's clean

0

This is a solution with name of the page :

Accueil is the name of the page.

    $screens = ['Accueil'];
    foreach ($screens as $screen) {
     if ( get_the_title() == $screen2 ) {
     add_meta_box(
      'accueil_données',
      'Informations sur Orchestre',
      'metabox_accueil_infos',
      'page',
      'normal',
      'high'
     );
   }
}
-1
function mcf_add_custom_metabox() {
    global $post;
    if ( $_REQUEST['post'] == 30 ) {
        add_meta_box( 'shop_editor', ' ', 'mcf_callback', 'page' );
    }
}
add_action( 'add_meta_boxes_page', 'mcf_add_custom_metabox' );

I got a way to add metabox in specific page using page id.

  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Dec 15 '16 at 7:26
  • Hard coding is never a good idea. ;) – dhuyvetter Jun 5 '17 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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