简体   繁体   中英

Posts not getting sorted correctly in category.php and pagination

The problem is that when i access Category1, i should see only the posts from Category1, instead I see all posts from all categories (5 posts in total). When I access Category2, i can see only 2 posts from Category1, same thing for Category3 were i can see the same last 2 posts of Category1. The links for requesting each category is ?cat=3 , ?cat=1 , ?cat=5 . So it's requesting correctly the categories but not the posts from them.

My functions.php - for pagination

function cleanmean_numeric_posts_nav() {

if( is_singular() )
    return;

global $wp_query;

if( $wp_query->max_num_pages <= 1 )
    return;

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max   = intval( $wp_query->max_num_pages );

if ( $paged >= 1 )
    $links[] = $paged;

if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
}

echo '<div class="blogwrap"><div class="blogpagination"><ul>' . "\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
    printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? ' class="active"' : '';

    printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

    if ( ! in_array( 2, $links ) )
        echo '<li>…</li>';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
    $class = $paged == $link ? ' class="active"' : '';
    printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
    if ( ! in_array( $max - 1, $links ) )
        echo '<li>…</li>' . "\n";

    $class = $paged == $max ? ' class="active"' : '';
    printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}

/** Next Post Link */
if ( get_next_posts_link() )
    printf( '<li>%s</li>' . "\n", get_next_posts_link() );

echo '</ul></div></div>' . "\n";

}

category.php

<?php 
  $paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
  $rescat = new wp_query("posts_per_page=".$settings['numberofpostscat']."&paged=".$paged);?>
<?php if ( $rescat->have_posts() ) : ?> 
<?php while ( $rescat->have_posts() ) : $rescat->the_post(); ?>
Here goes some of my code, not of interest
<?php endwhile; ?>
<?php cleanmean_numeric_posts_nav(); ?>
<?php else: endif; ?>
<?php wp_reset_query(); ?>

Pagination is requesting like this ?cat=3&paged=2 . So I believe that it's something that has to do with $rescat = new wp_query("posts_per_page=".$settings['numberofpostscat']."&paged=".$paged);?> I think I should specify somehow the category but the thing that it's already getting triggered correctly, so don't know what to do, or if not somehow to get the correct posts. In Category1 there are 3 posts, in category 2 and 3 only one post.

Found my answer, needed to get the current category name like this:

$cat_name = get_category(get_query_var('cat'))->name;
$rescat = new wp_query("category_name=$cat_name&posts_per_page=
".$settings['numberofpostscat']."&paged=".$paged);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM