Use wp_list_pages custom walker to show only post thumbnails

In this example the project required the featured image thumbnail to display in the menu for every page, so rather than displaying a list of links to pages, there is a menu of images. An advanced function which extends the walker class.

/**
 * Snippet Name: Use wp_list_pages custom walker to show only post thumbnails
 * Snippet URL: https://www.wpcustoms.net/snippets/custom-walker-post-thumbnails/
 */
  // create our custom thumbnail walker
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 ) );
 // here comes the fun part where we insert our featured post image sized at 80x80px
        $output .= $indent . '
  • ‘ . $link_before . apply_filters( ‘the_title’, ” ) . $link_after . get_the_post_thumbnail($page->ID, array(80,80)) .’‘;if ( !empty($show_date) ) {
    if ( ‘modified’ == $show_date )
    $time = $page->post_modified;
    else
    $time = $page->post_date;

    $output .= ” ” . mysql2date($date_format, $time);
    }
    }
    }

    // grab the original wp_list_pages function and rename it to keep defaults working
    function thumbnail_pages($args = ”) {
    $defaults = array(
    ‘depth’ => 0, ‘show_date’ => ”,
    ‘date_format’ => get_option(‘date_format’),
    ‘child_of’ => 0, ‘exclude’ => ”,
    ‘title_li’ => __(‘Pages’), ‘echo’ => 1,
    ‘authors’ => ”, ‘sort_column’ => ‘menu_order, post_title’,
    ‘link_before’ => ”, ‘link_after’ => ”, ‘walker’ => ”,
    );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    $output = ”;
    $current_page = 0;

    // sanitize, mostly to keep spaces out
    $r[‘exclude’] = preg_replace(‘/[^0-9,]/’, ”, $r[‘exclude’]);

    // Allow plugins to filter an array of excluded pages (but don’t put a nullstring into the array)
    $exclude_array = ( $r[‘exclude’] ) ? explode(‘,’, $r[‘exclude’]) : array();
    $r[‘exclude’] = implode( ‘,’, apply_filters(‘wp_list_pages_excludes’, $exclude_array) );

    // Query pages.
    $r[‘hierarchical’] = 0;
    $pages = get_pages($r);

    if ( !empty($pages) ) {
    if ( $r[‘title_li’] )
    $output .= ‘

‘;
}

$output = apply_filters(‘wp_list_pages’, $output, $r);

if ( $r[‘echo’] )
echo $output;
else
return $output;
}

// usage:

$thumbmenu = new Thumbnail_walker();
$defaults = array(
‘depth’ => 0,
‘show_date’ => ”,
‘date_format’ => get_option(‘date_format’),
‘child_of’ => 0,
‘exclude’ => ”,
‘title_li’ => __(‘Pages’),
‘echo’ => 1,
‘authors’ => ”,
‘sort_column’ => ‘menu_order, post_title’,
‘link_before’ => ”,
‘link_after’ => ”,
‘walker’ => $thumbmenu, // this fires our new thumbnail walker menu
);

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x