dongmi5015 2015-09-03 16:23
浏览 92
已采纳

如何将分页添加到wordpress站点

Really struggling to add pagination to my wordpress blog posts.

I've seen a lot of tutorials but they all seem to do the problem that I'm having now.

If I add the function to the function.php file and then call it in my post I cant get the pagination to output onto the page.

The blogs allowed at the moment are 2 per page for testing purposes.

Function:

function twentyeleven_content_nav( $nav_id ) {

  global $wp_query;

  if ( $wp_query->max_num_pages > 1 ) : ?>

  <nav id="<?php echo $nav_id; ?>">
    <h3 class="assistive-text"><?php _e( 'Post navigation',     'twentyeleven' ); ?></h3>
    <div class="nav-previous"><?php next_posts_link( __( '<span     class="meta-nav">←</span> Older posts', 'twentyeleven' ) ); ?></div>
    <div class="nav-next"><?php previous_posts_link( __( 'Newer posts     <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></div>
  </nav><!-- #nav-above -->

  <?php endif;
}

Post loop php:

        <?php
            $custom_posts = new WP_Query( array(
                'order_by' => 'date'
            ));
            if ( $custom_posts->have_posts() ) : 
            while ( $custom_posts->have_posts() ) : $custom_posts->the_post(); $loopcounter++;

                if ($loopcounter == 1) {

                    get_template_part( 'content', 'first-post' );

                } else {
                    get_template_part( 'content', get_post_format() );
                }

            endwhile;
                twentyeleven_content_nav( nav );
            else :
                get_template_part( 'content', 'none' );
            endif;
    ?>

Let me know if you need any further information

PS want this to be custom NO PLUGINS thanks

****** UPDATE ******

        <?php
        $custom_posts = new WP_Query( array(
            'order_by' => 'date'
        ));

        if ( $custom_posts->have_posts() ) : 
            while ( $custom_posts->have_posts() ) : $custom_posts->the_post(); $loopcounter++;

                if ($loopcounter == 1) {

                    get_template_part( 'content', 'first-post' );

                } else {
                    get_template_part( 'content', get_post_format() );
                }

            endwhile;

            if(function_exists('pagenavi')) { pagenavi(); };

            else :
                get_template_part( 'content', 'none' );
            endif;
    ?>
  • 写回答

2条回答 默认 最新

  • doushi8186 2015-09-04 14:05
    关注

    I've fixed this with the below solution:

    <?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
    
        <!-- Defining the loop counter -->
        <?php $loopcounter = 0; ?>
    
        <!-- Looping through the blog posts -->
        <?php if( have_posts() ): ?>
    
            <?php while( have_posts() ): the_post(); $loopcounter++; ?>
    
                    <?php if ($loopcounter === 1) {
    
                        get_template_part( 'content', 'first-post' );
    
                    } else {
                        get_template_part( 'content', get_post_format() );
                    } ?>
    
            <?php endwhile; ?>
    
            <!-- Adds navigation to older or newer posts -->
            <div class="navigation">
                <span class="newer left">
                    <?php previous_posts_link(__('Newer posts','example')) ?>
                </span> 
                <span class="older right">
                    <?php next_posts_link(__('Older posts','example')) ?>
                </span>
            </div>
    
        <?php else: ?>
    
            <?php get_template_part( 'content', 'none' ); ?>
    
        <?php endif; wp_reset_query(); ?>
    

    Reference from this link: https://upthemes.com/blog/2011/07/how-to-build-a-custom-page-template-for-blog-posts/

    Thanks Mark for your help

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀