douxing5199 2016-02-17 05:16
浏览 28
已采纳

怎么把DESC编号放在帖子标题wordpress友好页面导航之前?

I put a number before the post title in Wordpress and number ordering is desc.

This code in my theme's 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>

My problem, it's code does not function if I use page navigation. If I click page2, page3, ..., it's numbering don't show real total post.

Any help me, please. What code should I add and placed where?

  • 写回答

2条回答 默认 最新

  • drgc9632 2016-02-17 07:42
    关注

    For this to work properly, we need:

    • The current post position

    • Total amount of posts

    • Amount of posts per page

    • The current page we are on

    Lets look at a possible function to achieve this

    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 );
    }
    

    You can now add it anywhere inside the loop as follow

    echo get_post_position_in_reverse();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?