显示上一篇文章中的特色图片

时间:2018-05-03 作者:Maanstraat

我正在构建一个类别概述页面,并使用以下代码:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ($categories as $category):
        if ($counter < $limit) {
            $args  = array(
                \'category__in\' => array(
                    $category->term_id
                ),
                \'caller_get_posts\' => 1
            );
            $posts = get_posts($args);
            if ($posts) {
                echo \'<div class="category">\';
                echo \'<a href="\' . get_category_link($category->term_id) . \'" title="\' . sprintf(__("View all posts in %s"), $category->name) . \'" \' . \'>\';
                echo \'<h3>\' . $category->name . \'</h3>\';
                echo \'</a>\';
                echo \'</div>\';

            }
        }

    $counter++;
    endforeach;
?>
但是如何在回音中插入该类别最后一篇文章的特色图像。

2 个回复
最合适的回答,由SO网友:Maanstraat 整理而成

我用过Mat 并修复了一些重复的问题。

The code that works like a charm:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ($categories as $category):
        if ($counter < $limit) {
            $args  = array(
                \'posts_per_page\' => 1,
                \'cat\' => $category->cat_ID,
                \'ignore_sticky_posts\' => 1
            );
            $posts = get_posts($args);
            if ($posts) {
                    echo \'<div class="category">\';
                    while( have_posts() ) : the_post();
                        the_post_thumbnail();
                    endwhile;
                    echo \'<a href="\' . get_category_link($category->term_id) . \'" \' . \'>\';
                    echo \'<h3>\' . $category->name . \'</h3>\';
                    echo \'</a>\';
                    echo \'</div>\';
            }
        }

    $counter++;
    endforeach;
?>

SO网友:Mat

这应该行得通。我修改了你的$argswhile(have_posts()) : the_post(); 循环,然后添加the_post_thumbnail() 功能,最后wp_query_reset(); 在底部:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ( $categories as $category ):
        if ( $counter < $limit ) {
            $args  = array(
                \'posts_per_page\' => 1,
                \'cat\' => $category->cat_ID,
                \'ignore_sticky_posts\' => 1
            );
            $posts = get_posts( $args );

            while( have_posts() ) : the_post();
                echo \'<div class="category">\';
                the_post_thumbnail();
                echo \'<a href="\' . get_category_link($category->term_id) . \'" title="\' . sprintf(__("View all posts in %s"), $category->name) . \'" \' . \'>\';
                echo \'<h3>\' . $category->name . \'</h3>\';
                echo \'</a>\';
                echo \'</div>\';
            endwhile;

        }

    $counter++;
    endforeach;

    wp_query_reset();

?>
Ps。caller_get_posts 自版本3.1以来已弃用!使用ignore_sticky_posts 而是在您的$args

结束

相关推荐

Media Library Categories

我使用以下内容将类别分配给我的WordPress媒体库(在functions.php):function wptp_add_categories_to_attachments() { register_taxonomy_for_object_type( \'category\', \'attachment\' ); } add_action( \'init\' , \'wptp_add_categories_to_attachments\' ); 每个媒体库项目都