我在Wordpress中的帖子标题前放了一个数字,数字排序是desc。
我的主题是index.php中的代码:
<div id="content">
<?php if ( have_posts() ) : $post_nr = $wp_query->post_count; ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php echo $post_nr--;?>. Book Title: <span class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>. Writer: <?php the_tags(' '); ?>. Publisher: <?php the_category(', '); ?>. </span>
</div>
<?php endwhile; ?>
<?php endif;?>
</div>
我的问题,如果我使用页面导航,它的代码不起作用。如果我点击page2,page3,...,它的编号不会显示真正的总帖子。
请帮助我。我应该添加什么代码并放在哪里?
答案 0 :(得分:1)
尝试使用函数而不是查询
$post_nr = wp_count_posts();
答案 1 :(得分:1)
为了使其正常工作,我们需要:
当前职位
帖子总数
每页发布的数量
我们当前的页面
让我们看看实现这个的可能功能
function get_post_position_in_reverse()
{
// Invoke the global $wp_query object
global $wp_query;
// Make sure that we are actually inside the loop, if not, bail
if ( !in_the_loop() )
return false;
//Setup our variables we will be using
// Get the current page we are on
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$current_page = get_query_var( 'page' );
} else {
$current_page = 1;
}
// Get the current post's position plus 1 as post counter starts at 0
$post_position = $wp_query->current_post + 1;
// Get the total amount of posts in the query
$total_posts = $wp_query->found_posts;
// Get the amount of posts_per_page from backend
$ppp = get_option( 'posts_per_page' );
/**
* Now that we have everything set up, we need to do the maths
*
* If we have 20 posts across 4 pages with 6 posts per page, we will have
* 6 posts on pages 1,2 and 3 and only 2 posts on page 4. Our post numbers
* will be as follow, the first post on page one will be 20 and the last
* post on page 4 will be 1
*/
$number = $total_posts - ( ( $current_page * $ppp ) - ( $ppp - $post_position ) ) + 1;
return number_format_i18n( $number );
}
您现在可以将其添加到循环内的任何位置,如下所示
echo get_post_position_in_reverse();