首页 > 解决方案 > WordPress:posts_per_page 在存档页面上不起作用

问题描述

我有一个自定义循环,它应该以随机顺序显示每个类别的 3 个项目。问题是,循环总是显示该类别中的所有项目。

我已经在一个页面上测试了代码,它的工作方式应该是这样,并且只显示了 3 个项目。

所以我猜它与存档页面有关?!

这是循环本身:

<?php

        $args_thema = array (
            'post_type' => array( 'project' ),
            'orderby' => 'rand',
            'posts_per_page'    => 3,
            'paged'             => $paged,
            //'ignore_sticky_posts'    => 1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project-category',
                    'field'    => 'slug',
                    'terms'    => $category->slug,
                ),
            ),
        );

        $query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
        <ul class="">
            <?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
                <li class="">
                    <a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                        <?php the_title(); ?>
                    </a>
                </li>
            <?php endwhile; ?>
        </ul>
        <?php else : ?>

        <?php endif; wp_reset_postdata(); ?>

这是完整的代码(我想不是那么相关):

<?php

    $args       = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
    $categories = get_categories($args);

    foreach($categories as $category) {

?>
<div class="row">
    <div class="col-lg-4">

        <?php
            $term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
            $args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
        ?>

        <h4><a href="<?php echo get_term_link($category->slug, 'project-category')  ?>" title="<?php echo $category->name ?>"><?php echo $category->name; ?></a></h4>

    </div>
    <div class="col-lg-8">

        <?php

        $args_thema = array (
            'post_type' => array( 'project' ),
            'orderby' => 'rand',
            'posts_per_page'    => 3,
            'paged'             => $paged,
            //'ignore_sticky_posts'    => 1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'project-category',
                    'field'    => 'slug',
                    'terms'    => $category->slug,
                ),
            ),
        );

        $query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
        <ul class="">
            <?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
                <li class="">
                    <a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                        <?php the_title(); ?>
                    </a>
                </li>
            <?php endwhile; ?>
        </ul>
        <?php else : ?>

        <?php endif; wp_reset_postdata(); ?>

    </div>
</div>

<?php } ?>

标签: phpwordpress

解决方案


抱歉,这是一个额外的功能,将存档帖子设置为 99。

而不是is_post_type_archive我用is_archive


推荐阅读