POSTS_PER_PAGE不按术语工作

时间:2019-01-23 作者:Max

我试图限制帖子数量,但没有用。它显示了它们的全部。

为什么?posts_per_page 这里不行吗?

<?php // Output all Taxonomies names with their respective items
$terms = get_terms( \'genres\' );
foreach( $terms as $term ):
?>
    <h3><?php echo $term->name; // Print the term name ?></h3>
    <ul>
        <?php
        $posts = get_posts( array(
            \'post_type\'      => \'product\',
            \'posts_per_page\' => 1, 
            \'taxonomy\'       => $term->taxonomy,
            \'term\'           => $term->slug,                                  
        ) );
        foreach ( $posts as $post):  // begin cycle through posts of this taxonmy
            setup_postdata( $post ); // set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
    </ul>

1 个回复
SO网友:Mohsin Ghouri

我认为你用错了税务查询。请使用\'tax_query\' => array( array( \'taxonomy\' => \'$term->taxonomy\', \'field\' => \'slug\', \'terms\' => \'$term->slug\' ) ),

而不是\'taxonomy\' => $term->taxonomy, \'term\' => $term->slug,

它会很好地工作。