简体   繁体   中英

Wordpress wp_list_pages walker with featured image captions

I have created a custom walker for wp_list_pages() in Wordpress. This walker displays the featured images of the pages in question. My problem is that I need to also show the caption of these featured images. At the moment this is just showing the title of the page, I don't know how to get the featured image caption instead.

Here is my walker:

class SlideshowPics_walker extends Walker_page {
    function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
        if(has_post_thumbnail($page->ID)){
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'main-featured-thumbnail' );
            $link_title = $link_before . '<img src="'.$image[0].'" alt="'.esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ).'"/><div class="caption">'.apply_filters( 'the_title', $page->post_title, $page->ID ). $link_after.'</div>';
        } else
        $link_title= $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_title . '</a>'
        .'</li>';
    }
}

Solved this by using:

class SlideshowPics_walker extends Walker_page {
    function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
        if(has_post_thumbnail($page->ID)){
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'main-featured-thumbnail');
            $imageCap = get_post(get_post_thumbnail_id($page->ID))->post_excerpt;
            $link_title = $link_before . '<img src="'.$image[0].'" alt="'.esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ).'"/><div class="caption">'.$imageCap. $link_after.'</div>';
        } else
        $link_title= $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_title . '</a>'
        .'</li>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM