[英]How to display all posts in category.php sorted in descendent order by the date the post was added
[英]Posts not getting sorted correctly in category.php and pagination
问题是,当我访问Category1时,我应该只看到Category1中的帖子,而是看到所有类别中的所有帖子(总共5个帖子)。 当我访问Category2时,我只能从Category1中看到2条帖子,对于Category3来说,我可以看到相同的后2条相同的帖子。 用于请求每个类别的链接是?cat=3
, ?cat=1
, ?cat=5
。 因此,它要求的是正确的类别,而不是类别的帖子。
我的functions.php-分页
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(); ?>
分页请求是这样的?cat=3&paged=2
。 因此,我认为这与$rescat = new wp_query("posts_per_page=".$settings['numberofpostscat']."&paged=".$paged);?>
我认为我应该以某种方式指定类别,但已经正确触发的事情,所以不知道该怎么办,或者不知道如何获得正确的帖子。 在类别1中有3个帖子,在类别2和3中只有一个帖子。
找到了我的答案,需要获得当前的类别名称,如下所示:
$cat_name = get_category(get_query_var('cat'))->name;
$rescat = new wp_query("category_name=$cat_name&posts_per_page=
".$settings['numberofpostscat']."&paged=".$paged);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.