WordPress循环分组

时间:2018-09-08 14:27:20

标签: php wordpress

我想用连续编号循环<li>标签中的<ul>标签,但是它在每个<ul>标签中重复编号。 这是我的代码:

$catquery = new WP_Query( 'cat=6&posts_per_page=2' );
if ( $catquery->have_posts() ) {
 if ( is_home() && is_front_page() ) {
         while ( $catquery->have_posts() ) {
             $catquery->the_post();
             get_template_part( 'template-parts/single-song', get_post_type() );
         }
 }
}

$catquery = new WP_Query( 'cat=6&posts_per_page=2' );
if ( $catquery->have_posts() ) {
 if ( is_home() && is_front_page() ) {
         while ( $catquery->have_posts() ) {
             $catquery->the_post();
             get_template_part( 'template-parts/single-song', get_post_type() );
         }
 }
}
wp_reset_postdata();

输出为:

<ul>
    <li>Post 1</li>
    <li>Post 2</li>
</ul>
<ul>
    <li>Post 1</li>
    <li>Post 2</li>
</ul>

简而言之,我的预期输出应该是这样的:

<ul>
    <li>Post 1</li>
    <li>Post 2</li>
</ul>
<ul>
    <li>Post 3</li>
    <li>Post 4</li>
</ul>

template-parts / single-song文件包含:

    <li></li>

1 个答案:

答案 0 :(得分:1)

您应该使用查询分页或偏移量。另外,对参数使用数组语法:

分页:

$catquery = new WP_Query(
    array(
        'cat' => 6,
        'posts_per_page' => 2,
        'paged' => 2
    )
);

带偏移量:

$catquery = new WP_Query(
    array(
        'cat' => 6,
        'posts_per_page' => 2,
        'offset' => 2
    )
);

参考:https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters