change slug for multiple pages with same root

  • dianaascher

    #7641

    Hi there,

    Sorry if this question is naive, but I can’t seem to figure it out. I took Syahir’s code to trigger a custom header for a single page:

    <?php  /* Only add this line if you're working on an empty functions.php file */
    /**
    * Manually set different header image for individual pages
    */
    function graphene_custom_header_image( $img_url ){
    /* Replace page-slug with the actual page slug */
    if ( is_page( 'page-slug' ) ) return 'http://www.new-image.url';

    return $img_url;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image' );

    and it works perfectly. I’m wondering whether I can create a wildcard indicator for any page that includes the slug. In other words, if the primary page slug for the page I want to have the custom header is” a-main”, can I use something like if ( is_page(‘a-main/*)) to load the custom header on all pages with that root?

    http://artemisediting.com

    specifically, http://artemisediting.com/a-main/

    Thank you,

    Diana

    Admin

    Syahir Hakim

    #38848

    Try this:

    /**
    * Manually set different header image for individual pages
    */
    function graphene_custom_header_image( $img_url, $post_id = false ){
    if ( ! $post_id ) return $img_url;

    $current_post = get_post( $post_id );
    if ( stristr( $current_post->post_name, 'a-main' ) !== false ) return 'http://www.new-image.url';

    return $img_url;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image', 10, 2 );

    dianaascher

    #38849

    Thanks, Syahir,

    It still works for the top page, but not for the others. Here’s what I entered, just in case I made a stupid mistake:

    /*** Manually set different header image for individual pages*/

    function graphene_custom_header_image( $img_url, $post_id = false ){
    if ( ! $post_id ) return $img_url;

    $current_post = get_post( $post_id );
    if ( stristr( $current_post->post_name, 'a-main' ) !== false ) return 'http://artemisediting.com/wp-content/uploads/2013/04/bspaheader.png';

    return $img_url;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image', 10, 2 );

    I’m also curious: what do the 10 and 2 signify?

    Thanks so much,

    Diana

    Admin

    Syahir Hakim

    #38850

    Ah… in your site there is only one page that contains a-main in its slug. The other pages are children of that page, but do not have a-main in their slugs. Try this then:

    /**
    * Manually set different header image for individual pages
    */
    function graphene_custom_header_image( $img_url, $post_id = false ){
    if ( ! $post_id ) return $img_url;

    $parent_id = 65; /* Change to the ID of the parent page */
    $current_post = get_post( $post_id );
    if ( in_array( $parent_id, array( $current_post->ID, $current_post->post_parent ) ) ) return 'http://www.new-image.url';

    return $img_url;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image', 10, 2 );

    10 is the priority for the function, while 2 is the number of arguments it accepts.

    http://codex.wordpress.org/Function_Reference/add_filter

    dianaascher

    #38851

    Ahhh. Or maybe I should just change the slugs to a-main-whatever?

    Admin

    Syahir Hakim

    #38852

    Did you try the last code I provided?

    dianaascher

    #38853

    yes, but got this:

    Parse error: syntax error, unexpected T_RETURN in /home/content/c/o/r/corporatelogin/html/artemisediting/wp-content/themes/dianas-graphene/functions.php on line 10

    looking for extra spaces

    Admin

    Syahir Hakim

    #38854

    Ah, sorry the code was missing a single closing parentheses. I’ve corrected the above code. Please try again.

    dianaascher

    #38855

    It doesn’t seem to do anything. 🙁

    Admin

    Syahir Hakim

    #38856

    Did you change the $parent_id to the actual ID of the parent page, i.e. the page with the a-main slug?

    dianaascher

    #38857

    Thank you! You really saved my bacon. So grateful!!!

    dianaascher

    #38858

    So now that the function identifies the pages that should have the same header, can I use that same format to change things like sidebar colors, etc.? Is there a standard format I can apply?

    Prasanna SP

    #38859
    Quote:
    ..can I use that same format to change things like sidebar colors, etc.?

    Since each page has an unique ID, you can change sidebar colours etc, with just Custom CSS.

    dianaascher

    #38860

    Yes, each page has a unique ID, but the site will evolve with new pages and posts and I doubt my client could go in and ad new IDs to the CSS. I was thinking that now that there is a mechanism to identify the pages that require the secondary style, there could be some way to change items in addition to the header.

    Admin

    Syahir Hakim

    #38861

    You can add this to the child theme’s functions.php file:

    /**
    * Add custom body class
    */
    function my_body_class( $classes ) {
    $parent_id = 65; /* Change to the ID of the parent page */
    $current_post = get_post( $post_id );
    if ( in_array( $parent_id, array( $current_post->ID, $current_post->post_parent ) ) ) $classes[] = 'a-main';

    return $classes;
    }
    add_action( 'body_class', 'my_body_class' );

    That will add the .a-main class to the <body> element, so you can use that to prefix the CSS codes to be applied to those pages.

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.