WordPress.org

Ready to get started?Download WordPress

Forums

Add metabox to a "Page" that uses a specific template (5 posts)

  1. RyanJBaron
    Member
    Posted 3 years ago #

    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.

  2. RyanJBaron
    Member
    Posted 3 years ago #

    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");
      }
  3. tbradley22
    Member
    Posted 2 years ago #

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

  4. apascal07
    Member
    Posted 2 years ago #

    Works beautifully! Thanks for posting the solution.

  5. apascal07
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic