WordPress:posts_per_page在存档页面上不起作用

时间:2019-05-16 13:41:48

标签: php wordpress

我有一个自定义循环,该循环应按随机顺序显示每个类别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 } ?>

1 个答案:

答案 0 :(得分:0)

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

我使用is_post_type_archive而不是is_archive