WordPress.org

Ready to get started?Download WordPress

Forums

Show a div only in certain pages AND their sub-pages (7 posts)

  1. Carlo
    Member
    Posted 2 years ago #

    Ok, how can i do that?

    I have 4 main product pages, each one of 'em has some sub-pages.
    I want to show different content in the header for each of one of these for group of pages.

    Any idea?

    Thanks

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. dkotter
    Member
    Posted 2 years ago #

    I would use some WordPress conditional tags. You can check if you are on a certain page or on a sub page.

  4. Carlo
    Member
    Posted 2 years ago #

    Actually i was looking for a definition for sub-pages, because i keep adding them and i don't want to constantly (and manuallly) update my code with new ID.

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    Check against $post->post_parent if you are inside a Loop.

  6. Carlo
    Member
    Posted 2 years ago #

    I've found this code, which works fine for me, in theory. Actually, i can't make it work on subpages. Maybe bacause i dind't change the number beside the page name ('2', '56', '15')? What are they?

    <?php
    if ( is_page( 'about' ) || '2' == $post->post_parent ) {
        // the page is "About", or the parent of the page is "About"
        $bannerimg = 'about.jpg';
    } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) {
        $bannerimg = 'teaching.jpg';
    } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) {
        $bannerimg = 'admissions.jpg';
    } else {
        $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
    }
    ?>
  7. dkotter
    Member
    Posted 2 years ago #

    The numbers are the ID's of the pages. $post->post_parent returns the ID of the parent post. So you'll want to change those numbers to match the ID's of the correct pages/posts.

Topic Closed

This topic has been closed to new replies.

About this Topic