Theresia Vidi
Theresia Vidi

Reputation: 61

Wordpress loop grouping

I want to loop <li> tag in <ul> tag with continuous numbering but it repeats numbering in each <ul> tag. here's my code:

$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();

the output is:

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

in short my expected output should be like this:

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

the template-parts/single-song file contains:

    <li></li>

Upvotes: 0

Views: 28

Answers (1)

Rodrigo Brito
Rodrigo Brito

Reputation: 378

You should use query pagination or offset. Also, use the array syntax for parameters:

With pagination:

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

With offset:

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

Reference: https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

Upvotes: 1

Related Questions