对wp_list_ategories的修改

时间:2013-12-01 作者:andy

我把它挖出来是为了帮助我修改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类里面

有什么想法吗?

1 个回复
最合适的回答,由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

结束

相关推荐

尝试使用GET_CATEGORIES列出自定义分类的术语

我正在使用这段代码尝试列出一个自定义分类法的术语,以便在元框选择框中使用,但在我实现它时,它没有显示任何内容(尽管使用默认的“类别”分类法)$partners = array(); $partners_obj = get_categories(array(\'taxonomy\' => \'partner-cat\')); $partners[\'\'] = \'-\'; foreach ($partners_obj as $partner) { $partn