我把它挖出来是为了帮助我修改wp\\u list\\u类别,并将最新帖子中的特色图片放在生成的li中。
class CategoryThumbnail_Walker extends Walker_Category {
// A new element has been stumbled upon and has ended
function end_el( &$output, $category, $depth, $args ) {
// Output the standard link ending
parent::end_el( &$output, $category, $depth, $args );
// Get one post
$posts = get_posts( array(
// ...from this category
\'category\' => $category->cat_ID,
\'numberposts\' => 1
) );
// If a post has been found
if ( isset($posts[0]) ) {
// Get its thumbnail and append it to the output
$featured = get_the_post_thumbnail( $posts[0]->ID, \'extrathumb\', null );
$output .= $featured;
}
}
}
因此使用:-
<?php wp_list_categories( array(
\'show_count\' => 1,
\'walker\' => new CategoryThumbnail_Walker()) ); ?>
这几乎正是我想要的,输出特征图像以及类别名称和该类别的帖子数量。
唯一的问题是它将img输出到li之外,就像这样
<li class="cat-item cat-item-38"><a href="http://192.168.1.141:8888/category/around-the-web/" title="View all posts filed under Around The Web">Around The Web</a> <span class="post_count"> 1 </span>
</li>
<img width="79" height="79" src="http://192.168.1.141:8888/wp-content/uploads/2013/11/thanksgiving-day-parade-wind-79x79.jpg" class="attachment-extrathumb wp-post-image" alt="A lot could go awry.">
我需要那张照片在li类里面
有什么想法吗?
最合适的回答,由SO网友:s_ha_dum 整理而成
将您所称的“标准链接结束”移到函数的结尾,而不是开头。
class CategoryThumbnail_Walker extends Walker_Category {
// A new element has been stumbled upon and has ended
function end_el( $output, $category, $depth, $args ) {
// Get one post
$posts = get_posts( array(
// ...from this category
\'category\' => $category->cat_ID,
\'numberposts\' => 1
) );
// If a post has been found
if ( isset($posts[0]) ) {
// Get its thumbnail and append it to the output
$featured = get_the_post_thumbnail( $posts[0]->ID, \'extrathumb\', null );
$output .= $featured;
}
// Output the standard link ending
parent::end_el( $output, $category, $depth, $args );
}
}
wp_list_categories(
array(
\'show_count\' => 1,
\'walker\' => new CategoryThumbnail_Walker()
)
);
我相信“nest”是正确的(虽然我没有对其进行彻底的分析),但我非常确信通过修改
start_el
方法而不是
end_el
一