如何为WordPress主页添加分页

时间:2017-01-11 作者:Ashalatha

您好,我正在为我的网站使用Voux主题,因为我需要在主页中添加分页链接,如“旧帖子和新帖子”。下面是一个屏幕截图示例。enter image description here

实际上,现在我在菜单中获取这些链接的方式与在主页中显示这些链接的方式相同。

<?php get_header(); ?>
<?php 
$blog_featured = ot_get_option(\'blog_featured\');
?>
<?php if ($blog_featured) { ?>
<div class="row header_content">
    <div class="small-12 columns">
        <?php 
            $args = array(
                \'p\' => $blog_featured,
                \'post_type\' => \'any\'
            );
            $featured_post = new WP_Query($args);
        ?>

        <?php if ($featured_post->have_posts()) :  while ($featured_post->have_posts()) : $featured_post->the_post(); ?>
        <?php get_template_part( \'inc/loop/blog-featured\' ); ?>
        <?php endwhile; else : endif; ?>
    </div>
</div>  
<?php } ?>

<div class="row">
<section class="blog-section small-12 medium-8 columns">
    <div class="row" data-equal=">.columns">
        <?php if (have_posts()) :  while (have_posts()) : the_post(); ?>
            <?php get_template_part( \'inc/loop/blog-list\' ); ?>
        <?php endwhile; else : ?>
            <?php get_template_part( \'inc/loop/notfound\' ); ?>
        <?php endif; ?>
    </div>
    <?php if ( get_next_posts_link() || get_previous_posts_link()) { ?>
    <div class="blog_nav">
        <?php if ( get_next_posts_link() ) : ?>
            <a href="<?php echo next_posts(); ?>" class="next"><i class="fa fa-angle-left"></i> <?php _e( \'Older Posts\', \'thevoux\' ); ?></a>
        <?php endif; ?>

        <?php if ( get_previous_posts_link() ) : ?>
            <a href="<?php echo previous_posts(); ?>" class="prev"><?php _e( \'Newer Posts\', \'thevoux\' ); ?> <i class="fa fa-angle-right"></i></a>
        <?php endif; ?>
    </div>
    <?php } ?>
</section>
<?php get_sidebar(); ?>
</div>

4 个回复
SO网友:Shafiq

我的博客上也需要类似的东西。我使用以下代码添加了必要的链接(根据需要进行调整)

<?php previous_posts_link(__( \'<button class="btn btn-primary pull-left"><i class="fa fa-long-arrow-left"></i>&nbsp; Newer Posts</button>\' )) ?>
<?php next_posts_link(__( \'<button class="btn btn-primary pull-right">Older Posts &nbsp;<i class="fa fa-long-arrow-right"></i></button>\' )) ?>

SO网友:Shafiq

找到另一个选项,如下页所示:https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress

在主题函数的末尾添加以下代码。php文件

function pagination_nav() {
    global $wp_query;

    if ( $wp_query->max_num_pages > 1 ) { ?>
        <nav class="pagination" role="navigation">
            <div class="nav-previous"><?php next_posts_link( \'&larr; Older posts\' ); ?></div>
            <div class="nav-next"><?php previous_posts_link( \'Newer posts &rarr;\' ); ?></div>
        </nav>
<?php }
}
然后在主题中添加以下行(需要显示分页的地方)

<?php pagination_nav(); ?>
这通常是您的索引。php文件。向下滚动到循环结束之前。这在“endwhile”语句之后,但在循环中的任何“else”语句之前。

SO网友:Jayesh

在函数中输入以下代码。php文件

function numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there\'s only 1 page */
    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 );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

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

    echo \'<div class="navigation"><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>\' . "\\n";

}
并将这一行放在主题索引中。php,存档。php,类别。php和任何其他希望看到分页的归档页面模板。

<?php numeric_posts_nav(); ?>

SO网友:vishal verma

这是forntpage分页的正确工作代码

if ( get_query_var(\'paged\') ) {
    $paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
    $paged = get_query_var(\'page\');
} else {
    $paged = 1;
}

$args = array( 
    \'post_type\'      => \'post\', 
    \'posts_per_page\' => 8, 
    \'post_status\'    => \'publish\',
    \'paged\'          => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title(); the_content();
endwhile;
和分页代码

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
  \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
  \'format\' => \'?paged=%#%\',
  \'current\' => max( 1, get_query_var(\'page\') ),
  \'total\' => $loop->max_num_pages
) );

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请