Site logo

Archived topic

Content nav only for posts in a category

9 replies · Started by Alberto on June 21, 2018

Viewing posts 1–10 of 10

Hello
I'm working with a chil theme so I use a home.php where I modify the loop with this code:

$args = array(
    'post_type' => 'post',
    'category_name'=> 'blog-marketing',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' )
);

$lazo = new WP_Query( $args );

if ( $lazo->have_posts() ) :

	while ( $lazo->have_posts() ) : $lazo->the_post();

		get_template_part( 'content', get_post_format() );

	endwhile;

	generate_content_nav( 'nav-below' );

else :

	get_template_part( 'no-results', 'archive' );

endif;

The problem is the navigation between pages, cause it counts all the posts.
I need it to works only with the posts filtered in my query.
How can I achieve that?

Thanks.

Hi there,

Just so I understand - the pagination is counting every single post, not just the posts in your specified category?

Are you sure your template is working? If you add some random code (echo 'hi';), do you see it on the page?

Hi Tom!
I suppose you're talking about home.php... and the answer is yes, of course, in fact the first paragraph you see ("Sección publicada por especialistas...") has been writed in a div into that file.

Instead of this:

$args = array(
    'post_type' => 'post',
    'category_name'=> 'blog-marketing',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' )
);

Try this:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type' => 'post',
    'category_name'=> 'blog-marketing',
    'posts_per_page' => 10,
    'paged' => $paged
);

Then after:

generate_content_nav( 'nav-below' );

Add this:

wp_reset_postdata();

Let me know :)

Hi Tom!
Not working yet. This is what I've got now in my home.php file

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type' => 'post',
    'category_name'=> 'blog-marketing',
    'posts_per_page' => 10,
    'paged' => $paged
);

$lazo = new WP_Query( $args );

echo '<div class="intro-blog">Sección publicada por especialistas en <strong>marketing digital</strong> y las <strong>redes sociales en China</strong>, tratando temas diversos desde herramientas de análisis, últimas noticias relacionadas con redes sociales y artículos relacionados con la traducción o interpretación, además se publicarán las últimas novedades de la empresa<strong> Traducciones Chino.</strong></div>';

if ( $lazo->have_posts() ) :

	while ( $lazo->have_posts() ) : $lazo->the_post();

		get_template_part( 'content', get_post_format() );

	endwhile;

	generate_content_nav( 'nav-below' );
	wp_reset_postdata();

else :

	get_template_part( 'no-results', 'archive' );

endif;

And as you can check in the blog, it still shows 17 pages (nothing after page 6).

And what happens if you replace:

generate_content_nav( 'nav-below' );

With:

the_posts_pagination();

It works the same way.

So strange, I would post your code (with the_posts_pagination()) on https://wordpress.stackexchange.com/.

Someone there might be able to spot the issue.

Let me know if they're not able to help :)

This archived topic is closed to new replies.