WordPress.org

Support

Support » Plugins and Hacks » Hacks » Add metabox to a "Page" that uses a specific template

Add metabox to a "Page" that uses a specific template

  • I was wondering if anybody knows how to add a custom meta box to a “page” that uses a specific template. I don’t need help creating the metabox or anything like that. I just want to be able to specify that if a “page” is using the template home-page.php that the custom meta box then show up on the back end. I found this post http://www.farinspace.com/page-specific-wordpress-meta-box/ and tried the code:

    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
      // check for a template type
      if ($template_file == 'home-page.php') {
        add_meta_box("faq_meta", "FAQ Options", "faq_meta", "page", "normal", "low");
      }

    But that didn’t seem to work. Removing the if statement, I am able to get my custom metabox to show up, but it shows up on all pages then.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Ok, I am going to answer my own question. Hopefully it will help someone else out. I was just missing 1 line of code, so the whole thing should look like this:

    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
      // check for a template type
      if ($template_file == 'home-page.php') {
        add_meta_box("faq_meta", "FAQ Options", "faq_meta", "page", "normal", "low");
      }

    tbradley22

    @tbradley22

    It did help someone else out! Thanks for posting the solution.

    Works beautifully! Thanks for posting the solution.

    Spoke too soon. Sadly, this only works if the page has been saved already. If you try to add a new page, you will receive undefined index and variables errors with WP_DEBUG on.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add metabox to a "Page" that uses a specific template’ is closed to new replies.
Skip to toolbar