Wordpress loop grouping
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:
<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();
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>
template-parts/single-song
1 Answer
1
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
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
can you show the code of
template-parts/single-song
template ?– Ivnhal
Sep 8 '18 at 14:40