Paul Podberezko
Paul Podberezko

Reputation: 35

Pagination not working with query_posts

I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.

Custom category code

        <?php query_posts('category_name=sport');

        if (have_posts()) : while (have_posts()) : the_post(); ?>
            <?php get_template_part('loop'); ?>
        <?php endwhile; // конец цикла
        else: echo '<p>Blank.</p>'; endif; ?>   

       <?php pagination(); ?>

Pagintion in function.php

function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
    'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))), 
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged')),
    'type' => 'array', 
    'prev_text'    => 'Назад',
    'next_text'    => 'Вперед',
    'total' => $wp_query->max_num_pages,
    'show_all'     => false, 
    'end_size'     => 4, 
    'mid_size'     => 4,
    'add_args'     => false,
    'add_fragment' => '',
    'before_page_number' => '',
    'after_page_number' => ''
));
if( is_array( $links ) ) {
    echo '<ul class="pagination">';
    foreach ( $links as $link ) {
        if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>"; 
        else echo "<li>$link</li>"; 
    }
    echo '</ul>';
 }

}

Please help

Upvotes: 2

Views: 4226

Answers (3)

Darsh khakhkhar
Darsh khakhkhar

Reputation: 676

instead of: query_posts('category_name=sport');

try this:

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'category_name' => 'sport',
    'posts_per_page' => 6,
    'paged' => $page
);

query_posts($args);

For further customization check the List of Query Vars

Upvotes: 4

PPL
PPL

Reputation: 6555

Please check below code:

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post_type' => 'work',
                'categories'=>'sports',
                'posts_per_page' => 2,
                'paged' => $page,
            );
            query_posts($args);

   if (have_posts()) : while (have_posts()) : the_post(); ?>
      <h3><?php the_title(); ?></h3>
      <?php the_content(); ?>
      <?php endwhile; 
      else: echo '<p>Blank.</p>'; endif; ?> 
     <?php pagination(); ?>

You have to mention post type also as per the example

Hope this work for you.

Upvotes: 0

M. Ferguson
M. Ferguson

Reputation: 1421

Try this:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 3,
  'paged'          => $paged,
  'category_name' => 'sport'
);

$the_query = new WP_Query( $args ); 
?>

Hope this helps :)

Upvotes: 0

Related Questions