How would I use Walker to modify wp_list_pages to display thumbnail only at lowest level in WordPress?

I’m working on a writer’s site. The pages are structured like so:

All Books
   Series
     Series A
        Book 1, Book 2, etc.
      Series B
        Book 1, Book 2, etc.
   Stand-alone novels
      Book 1, Book 2, etc.

Each book has its own page.

Read More

I can use wp_list_pages to generate lists of links at whatever level I want. What I need to do is use Walker classes to customize wp_list_pages like this, but so that only the lowest Book level uses the Featured Image (=book cover) for the link.

How do I do this? That is, how do I put in images only on the lowest level, which is not the same number of steps down from the top in different parts of the tree?

Related posts

Leave a Reply

1 comment

  1. Check if page has children:

    class Thumbnail_walker extends Walker_page {
        function start_el(&$output, $page, $depth, $args, $current_page) {
            if ( $depth )
                $indent = str_repeat("t", $depth);
            else
                $indent = '';
    
            extract($args, EXTR_SKIP);
            $css_class = array('page_item', 'page-item-'.$page->ID);
            if ( !empty($current_page) ) {
                $_current_page = get_page( $current_page );
                _get_post_ancestors($_current_page);
                if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
                    $css_class[] = 'current_page_ancestor';
                if ( $page->ID == $current_page )
                    $css_class[] = 'current_page_item';
                elseif ( $_current_page && $page->ID == $_current_page->post_parent )
                    $css_class[] = 'current_page_parent';
            } elseif ( $page->ID == get_option('page_for_posts') ) {
                $css_class[] = 'current_page_parent';
            }
    
            $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
    
            $children = get_children( 'post_type=page&numberposts=1&post_parent=' . $page->ID );
            if ( empty($children) ) {
                $link_contents = $link_before . apply_filters( 'the_title', '' ) . $link_after . get_the_post_thumbnail($page->ID, array(72,72));
            }
            else {
                $link_contents = $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after;
            }
            $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_contents .'</a>';
    
            if ( !empty($show_date) ) {
                if ( 'modified' == $show_date )
                    $time = $page->post_modified;
                else
                    $time = $page->post_date;
    
                $output .= " " . mysql2date($date_format, $time);
            }
        }
    }