简体   繁体   中英

Custom blog page in wordpress navigation Next and previous page not working

I have tried to create custom blog page with navigation. Everything works fine without navigation. I have tried fix the pagenation issue but it was not working below is the code for my custom page template.

     <?php /*
    Template Name: My Blog Page
    */
    get_header();
    ?>
    <div class="container-fluid"><div class="row bkg-grey"><div class="container"><div class="row">
<div class="col-md-8"><div class="row">
    <?php
    $args = array( 'post_type' => 'post');
if ( have_posts() ) : 
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  ?>
 <div class="col-md-12"><div class="row lwp-posts">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-4"><div class="row">
<?php 
if ( has_post_thumbnail() ) {
the_post_thumbnail('homepage-thumb',array('class' => 'img-responsive' ) );
} ?></div></div>
<div class="col-md-8"><div class="row">
<div class="rjs-post-title"><h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2></div>
<div class="rjs-post-meta"><span>Posted on :<?php the_time( 'j M Y' ); ?></span><span>Posted by : <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a></span><span itemprop="interactionCount" content='UserComments: <?php comments_popup_link( '0', '1', '%' ); ?> '>Comments : <?php comments_popup_link( '0', '1', '%' ); ?></span><span>Category : <?php the_category(', '); ?></span></div>
<?php the_excerpt(); ?>
</div></div>
<?php edit_post_link(); ?>
<?php wp_link_pages(); ?>
</article></div></div>
<?php endwhile; ?>
 <nav class="navigation index">
<div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
<div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
</nav>
</div></div>
<?php else :  endif; ?>
<div class="col-md-4"><div class="row">
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
</div></div></div></div></div></div>
<?php get_footer(); ?>

You need to add some parameters to your query args:

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 6,
  'paged' => $paged
);

And if you want to customize navigation links, use this pattern:

<nav class="navigation index">
  <?php 
    echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
        'add_args'     => false,
        'add_fragment' => '',
    ) );
  ?>
</nav>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM