0

I trying to add pagination to my WordPress website, it's working fine in main index page, I am trying to create different main pages for category issue with category pages, when click any link of pagination URL change but content still the same
index page code

<?php
if (have_posts()) :
    while (have_posts()) : the_post(); 

   /*  post code goes here */

     endwhile;


    the_posts_pagination( array(
    'mid_size' => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) ); 

    else :
        echo '<p>No content found</p>';

    endif;?>

category page code

<?php

query_posts('cat=5');
if (have_posts()) :
    while (have_posts()) : the_post(); 

   /*  post code goes here */

     endwhile;


    the_posts_pagination( array(
    'mid_size' => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) ); 

    else :
        echo '<p>No content found</p>';

    endif;?>

I also try this

<ul>
    <?php
    global $post;

    $myposts = get_posts( array(
        'offset'         => 1,
        'category'       => 5
    ) );

    if ( $myposts ) {
        foreach ( $myposts as $post ) : 
            setup_postdata( $post ); ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php
        endforeach;
        wp_reset_postdata();
    }

    the_posts_pagination( array(
    'mid_size' => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) ); 
    ?>
</ul>

but only show post, not the pagination

can someone help with this
thanks in advance for help

0

the_posts_pagination works with the global $wp_query, so your second method will not work.

query_posts modifies the main query but the arguments passed will overwrite any existing values, so all values needed for pagination are lost when you set cat=5. But you can preserve the existing values by modifying the global query string.

A more efficient way, however, would be to use the pre_get_posts action hook, e.g.:

function mysite_change_cat($query) {
  //if it is a category archive page and main query
  if ($query->is_category() && $query->is_main_query()) {
    $query->set('cat', 5);
  }
}
add_action('pre_get_posts', 'mysite_change_cat');

However if you are creating completely different pages for certain categories, then you could simply create template files for each of them named e.g. category-5.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.