-1

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. enter image description here

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>
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) Commented Feb 7, 2018 at 5:28

4 Answers 4

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>' )) ?>
6
  • Alternatively ... Consider using a wordpress plugin. e.g. - WP-PageNavi - wordpress.org/plugins/wp-pagenavi
    – Shafiq
    Commented Jan 11, 2017 at 12:29
  • Can you please share me where i need to add these code which you have sent here.I mean to say the file name
    – Ashalatha
    Commented Jan 11, 2017 at 12:31
  • It all depends on the theme. What theme are you using? or is it custom developed?
    – Shafiq
    Commented Jan 11, 2017 at 12:33
  • I am using thevoux theme
    – Ashalatha
    Commented Jan 11, 2017 at 12:33
  • can you mention where i need to add this code
    – Ashalatha
    Commented Jan 11, 2017 at 12:38
0

Found another option, as per the following page: https://www.elegantthemes.com/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.

3
  • Actually i have tried with the same code but it is not working
    – Ashalatha
    Commented Jan 11, 2017 at 12:45
  • I have updated my index.php file can you please update where i need to add these <?php pagination_nav();?>
    – Ashalatha
    Commented Jan 11, 2017 at 12:48
  • As I mentioned before it will usually be at the bottom - Have a look at how I implemented mine - pastebin.com/0GGAx8Tr It's obviously different... But it depends on the theme.
    – Shafiq
    Commented Jan 11, 2017 at 12:51
0

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(); ?>
4
  • Not working not getting links in home page i have added in index.php file
    – Ashalatha
    Commented Jan 11, 2017 at 13:14
  • this code is working completely in my project.
    – Jayesh
    Commented Jan 11, 2017 at 13:23
  • which kind of error are you getting when running this code?
    – Jayesh
    Commented Jan 11, 2017 at 13:24
  • It is not displaying any error
    – Ashalatha
    Commented Jan 12, 2017 at 4:56
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.