【问题标题】:posts_per_page only returning one postposts_per_page 只返回一篇文章
【发布时间】:2017-05-03 20:14:58
【问题描述】:

我有这个 wp_query,但它只返回 1 个帖子。我希望它返回带有标签“pick”的“目录”类别中的任何内容。我知道posts_per_page 可能存在问题,但没有任何效果(我已经搜索了很多)以使查询显示多个帖子。任何帮助都会很棒。

<?php // The Query

            wp_reset_query();

            global $wp_query;

            $term = $wp_query->queried_object;

            $args=array(
                'ignore_sticky_posts' => true,
                'posts_per_page' => -1,
                'post_type' => 'directory',
                'suppress_filters' => true,
                'tag' => 'picks',
                'tax_query' => array(
                array(
                    'taxonomy'  => $term->taxonomy,
                    'field'     => 'slug',
                    'terms'     => $term->slug,
                    )
                )
            );

            $new_query = new WP_Query($args);

        if ( $new_query->have_posts()) : $new_query->the_post(); ?>

            <article <?php post_class('post-tile'); ?>>
                <a href="<?php the_permalink() ?>" rel="bookmark">
                <?php
                    $post_thumbnail_id = get_post_thumbnail_id( );
                    $imagesized = wp_get_attachment_image_src( $post_thumbnail_id, 'big-post-thumb');
                    if ($imagesized[1] == 308) {
                        the_post_thumbnail('big-post-thumb');
                    } else {
                        the_post_thumbnail('cat-post-thumb');
                    }
                ?>
                <h3><?php the_title(); ?></h3>
                <div class="intro">
                    <p><?php echo get_excerpt(140); ?></p>
                </div>
                </a>
            </article>
            <?php wp_reset_postdata(); else : ?>
            <p>No picks as yet&hellip;</p>
        <?php endif; ?>

    </div>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    问题不在于posts_per_page,而在于缺少关键组件的循环。它没有while 声明。

    目前,您的所有循环都在显示第一篇文章。您需要 while 语句才能遍历所有帖子。

    改变这一行:

    if ( $new_query->have_posts()) : $new_query->the_post(); ?>
    

    到这里:

    if ( $new_query->have_posts() ) : while ( $new_query->have_posts() ) : $new_query->the_post(); ?>
    

    还有这一行:

    <?php wp_reset_postdata(); else : ?>
    

    到这里:

    <?php wp_reset_postdata(); endwhile; else : ?>
    

    【讨论】:

    • 赢家。谢谢你。我忘了给你道具。这里有一些道具。
    猜你喜欢
    • 2013-07-27
    • 2012-10-06
    • 2013-12-06
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多