php - How to add pagination for wordpress home page

Hi I am using thevoux theme for my website as i need to add pagination links like "Older Posts and Newer Posts"

Hi I am using thevoux theme for my website as i need to add pagination links like "Older Posts and Newer Posts" to home page.Here is an example screen shot how it should be.

Actually right now i am getting these links in menus in the same way i need to display them in home page as well.

<?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>

Hi I am using thevoux theme for my website as i need to add pagination links like "Older Posts and Newer Posts" to home page.Here is an example screen shot how it should be.

Actually right now i am getting these links in menus in the same way i need to display them in home page as well.

<?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>
Share Improve this question edited Jan 11, 2017 at 12:47 Ashalatha asked Jan 11, 2017 at 12:24 AshalathaAshalatha 11 silver badge2 bronze badges 1
  • dowwnvoted as it seems to be a theme specific question (at least there is nothing wrong with the snippet provided, nor there is an explanation of what was tried and how it failed) – Mark Kaplun Commented Feb 7, 2018 at 5:28
Add a comment  | 

4 Answers 4

Reset to default 0

I needed something similar on my blog too. I used the following code to add the necessary links (adjust as required)

<?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>' )) ?>

Found another option, as per the following page: https://www.elegantthemes/blog/tips-tricks/how-to-add-pagination-to-wordpress

Add the following code at the end of your theme's functions.php file

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

Then add the following line to your theme (where you need the pagination to be displayed)

<?php pagination_nav(); ?>

This will usually be your index.php file. Scroll down to just before the end of the loop. This is after the “endwhile” statement but before any “else” statements within the loop.

put this following code in your function.php file

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";

}

and put this line in your theme’s index.php, archive.php, category.php, and any other archive page template where you like to see pagination.

<?php numeric_posts_nav(); ?>

this is proper working code for pagination on 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;

and pagination code here

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744957179a4603262.html

相关推荐

  • php - How to add pagination for wordpress home page

    Hi I am using thevoux theme for my website as i need to add pagination links like "Older Posts and Newer Posts"

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信