0

I'm trying to add next/prev links to my wordpress post. This is the code for it:

<div id="demo">
<?php 
 $the_query = new WP_Query( array(
 'category_name' => 'Case Study',
  'posts_per_page' => 1,
 )); 
  ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php endwhile; ?>
 <?php wp_reset_postdata(); ?>
<div class="nav-previous alignleft"><?php next_post_link('&laquo; %link', '%title', TRUE); ?></div>
<div class="nav-next alignright"><?php previous_post_link('&laquo; %link', '%title', TRUE); ?></div>

<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>

However, this doesn't work and the next/prev links don't show. Any ideas what I'm doing wrong?

  • Did you try to review it using inspect element? Try to make WP_DEBUG ON in wp configuration file to see the actual error on the page. Let me know if you can share the URL to review with me. – Prashant Rawal May 29 '17 at 12:41
1

I made some updates in your code. Please, try this updated version.

 <div id="demo">
    <?php 
     $wp_query = new WP_Query( array(
     'category_name' => 'Case Study',
      'posts_per_page' => 1,
     )); 
      ?>
    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : the_post(); ?>

    <?php the_title(); ?>
    <?php the_content(); ?>

    <?php endwhile; ?>

    <div class="nav-previous alignleft"><?php next_post_link('&laquo; %link', '%title', TRUE); ?></div>
    <div class="nav-next alignright"><?php previous_post_link('&laquo; %link', '%title', TRUE); ?></div>
     <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php __('No News'); ?></p>
    <?php endif; ?>
    </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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