[Support request] Content nav only for posts in a category

Home Forums Support [Support request] Content nav only for posts in a category

Home Forums Support Content nav only for posts in a category

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #605046
    Alberto

    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.

    #605376
    Tom
    Lead Developer
    Lead Developer

    Hi there,

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

    #605446
    Alberto

    Hello Tom!
    Exactly!
    You can see it in traduccioneschino.es/blog
    It must only show 10 pages instead 17.

    Regards

    #605737
    Tom
    Lead Developer
    Lead Developer

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

    #605830
    Alberto

    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.

    #606186
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #606266
    Alberto

    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).

    #606475
    Tom
    Lead Developer
    Lead Developer

    And what happens if you replace:

    generate_content_nav( 'nav-below' );

    With:

    the_posts_pagination();

    #607827
    Alberto

    It works the same way.

    #608199
    Tom
    Lead Developer
    Lead Developer

    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 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.