WordPress.org

Forums

[resolved] Conditionals in functions.php (13 posts)

  1. slurve
    Member
    Posted 4 years ago #

    Anyone know how to get conditionals (like is_page_template) working in functions.php? I suppose this has something to do with the loop ... and that WP doesn't know the ID of the page? The first conditional below (post_id == '24') is working fine. The second one (is_page_template) is not. Any help would be greatly appreciated. Thanks.

    Tom

    function admin_init(){
        $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
        if ($post_id == '24') add_meta_box("lead_text", "Lead Text", "lead_text", "page", "normal", "high");
        if (is_page_template('page_staff_bio.php')) add_meta_box("bio_excerpt", "Bio Excerpt", "bio_excerpt", "page", "normal", "high");
      }
  2. slurve
    Member
    Posted 4 years ago #

    Anyone have an answer for this?

  3. esmi
    Forum Moderator
    Posted 4 years ago #

    Try:

    function admin_init(){
    	global $post;
        if ($post->ID == '24') add_meta_box("lead_text", "Lead Text", "lead_text", "page", "normal", "high");
        if (is_page_template('page_staff_bio.php')) add_meta_box("bio_excerpt", "Bio Excerpt", "bio_excerpt", "page", "normal", "high");
     }
  4. slurve
    Member
    Posted 4 years ago #

    Thanks for the response, esmi. Doesn't seem to work, though.

  5. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    First, don't name a function admin_init().

    Second, how is this function being called? Are you calling it explicitly in a template file somewhere, or are you hooking it into a filter?

    Third, what are you trying to accomplish with this function?

    I assume that you're trying to add a metabox to the post-edit screen for a specific post?

  6. slurve
    Member
    Posted 4 years ago #

    Chip, I'm hooking into admin_init. (Good call, I'll change the function name.) Basically, I'm trying to add the bio_excerpt meta box to all pages using the page_staff_bio.php template.

  7. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    Okay then, first things, first, here's how you properly hook into admin_init:

    function mytheme_add_bio_metabox() {
        // function code goes here
    }
    add_action( 'admin_init', 'mytheme_add_bio_metabox' );

    But actually, you don't want to hook that function into admin_init; you can specify a much more precise action: add_meta_boxes_{post-type}.

    The post-type in this case is "page", so you should use add_meta_boxes_page:

    function mytheme_add_bio_metabox() {
        // function code goes here
    }
    add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
  8. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    Next, we need to build out that function, to add the meta box. We're going to pass the $post global variable to the function, as we'll need it. We will determine if the current Page uses the appropriate template (as defined in the Post metadata), or if the Page has the appropriate ID. If so, we add our meta box:

    function mytheme_add_bio_metabox( $post ) {
        $template = get_post_meta( $post->ID, '_wp_page_template' ,true );
        if ( 'page_staff_bio.php' == $template || '24' == $post->ID ) {
            global $wp_meta_boxes;
            add_meta_box( 'bio_excerpt', 'Bio Excerpt', 'bio_excerpt', 'page', 'normal', 'high' );
        }
    }
    add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
  9. slurve
    Member
    Posted 4 years ago #

    Good to know about the add_meta_boxes_{post-type} action. Thanks. Is there a way to add the meta box only to pages using a certain template? Would I be able to use is_page_template() within mytheme_add_bio_metabox()?

  10. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    Is there a way to add the meta box only to pages using a certain template?

    (See my follow-up reply. :) )

  11. slurve
    Member
    Posted 4 years ago #

    Doh. Perhaps I need an afternoon cup of coffee. :) Thanks for the help, Chip.

  12. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    Good idea! I think I'll do the same. Let me know if the conditionals work as intended!

  13. slurve
    Member
    Posted 4 years ago #

    Worked like a champ. (Coffee and the code.) Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic