WordPress.org

Ready to get started?Download WordPress

Forums

Featured Image for 3rd level pages (6 posts)

  1. baalam
    Member
    Posted 2 years ago #

    Hi,

    I would like to automatically get the featured image from the parent page to the 2nd and 3rd child level pages.

    Second level page is not a problem (see hack below) but I cant get the 3rd level page to automatically get it.

    I'm using the fowolling code:

    <?php the_post_thumbnail( 'full' ); ?> // Regular call for the featured image
    
    <?php
    global $post;
    echo get_the_post_thumbnail($post->post_parent, 'full');
    ?> // Hack so the secundary or child page get the featured image from their parents

    With this code I get the same featured image I picked on the first level to be passed automatically to theird child page but not to the third level ones.

    • First Menu Item (Uses fi.jp as a featured image)
      • Child page 1 (gets same jpg using the hack from above)
        • Third page or second child page (won't get the featured image)
    • Second Menu Item

    Please help!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

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

    example; this will replace all of your posted code and should always show the post thumbnail of the top parent page:

    <?php echo get_the_post_thumbnail(array_pop(array_merge(array($post->ID), get_ancestors($post->ID,'page'))),'full'); ?>

  3. baalam
    Member
    Posted 2 years ago #

    alchymyth you are the man!

    Thanks a lot!

  4. orangjul
    Member
    Posted 1 year ago #

    I am trying to figure out how to do this same thing, but only if the child page does NOT have a featured image selected. In other words, if a child page has a featured image selected, it will display that featured image, but it does not have one selected, it will show the parent page's (or grandparent page's) featured image.

  5. orangjul
    Member
    Posted 1 year ago #

    I think I figured it out:

    <?php // Check if this is a post or page, if it has a thumbnail (featured image), and if it's a big one
    	if ( is_singular() &&
    		has_post_thumbnail( $post->ID ) &&
    		( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
    		$image[1] >= HEADER_IMAGE_WIDTH ) {
    		// Display featured image
    		echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    		// If there is no featured image selected, the page is a child page, and the parent page has a featured image
    		} else if((get_the_post_thumbnail('post-thumbnail') == '') && ($post->post_parent != '0') && get_the_post_thumbnail(array_pop(array_merge(array($post->ID), get_ancestors($post->ID,'page'))),'full') != '') {
    		// then show the parent page's featured image ?>
            <?php echo get_the_post_thumbnail(array_pop(array_merge(array($post->ID), get_ancestors($post->ID,'page'))),'full'); ?>
    		<?php } else { //Display default header image selected in Appeance > Header ?>
    		<img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
    <?php } ?>
  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    this will check if the page has a 'featured image' and then show it, otherwise it will show the 'featured image' of the top parent page;

    example: the page (a grandchild page) does not have a 'featured image' -> immediately get the image form the grand parent page.

    <?php
    if( !($img = get_the_post_thumbnail( $post->ID, 'full')) )
    $img = get_the_post_thumbnail(array_pop(array_merge(array($post->ID), get_ancestors($post->ID,'page'))),'full');
    echo $img;
    ?>

    if you need to gradually check the hierarchy for a 'featured image' (check page: if no image -> check parent (if exists): if no image -> check grandparent ...), try this:

    <?php
    $ancestors = array_merge(array($post->ID), get_ancestors($post->ID,'page'));
    do {
    $page_id = array_shift( $ancestors );
    $img = get_the_post_thumbnail( $page_id, 'full' );
    } while( !$img && $page_id );
    echo $img;
    ?>

    http://php.net/manual/en/control-structures.do.while.php
    http://php.net/manual/en/function.array-shift.php

Topic Closed

This topic has been closed to new replies.

About this Topic