POSTS_PER_PAGE在我这一端不起作用

时间:2021-05-19 作者:ddd

出于某种原因,我有这些代码行,posts_per_page 不工作。

<?php
        $posts_list = get_posts(
            array(
                \'posts_per_page\'    => 6,
                \'post_type\'         => \'post\',
                \'post_status\'       => \'publish\',
                \'paged\'             => $i_paged
            )
        );

        $the_query = new WP_Query( $posts_list );

        // The Loop
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {

                global $post;

                $the_query->the_post();

                $post_id = get_the_id();

                // Get fields
                $image      = get_the_post_thumbnail();
                $title      = get_the_title();
                $subtitle   = get_the_date();
                $content    = get_the_excerpt();
                $link       = get_permalink();

                $args[\'cards\'][] = array(
                    \'image\'         => $image,
                    \'title\'         => $title,
                    \'subtitle\'      => $subtitle,
                    \'content\'       => $content,
                    \'link\'          => $link,
                );

                ?>

            <?php }

        } else {
            // no posts found
        }

        echo \'<div class="posts-grid">\';
                get_template_part( \'/blocks/testimonial/testimonial-template\', null, $args );
        echo \'</div>\';

        /* Restore original Post Data */
        wp_reset_postdata();
        ?>

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

它不起作用,因为$posts_list 变量的返回值为get_posts() (即。$posts_list = get_posts( ... )), 因此$posts_list 实际上不是一个参数数组,而是posts 或空数组。

因此,要解决问题,请定义$posts_list 像这样:

$posts_list = array(
    \'posts_per_page\' => 6,
    \'post_type\'      => \'post\',
    \'post_status\'    => \'publish\',
    \'paged\'          => $i_paged
);