Search code examples
phpwordpressfooterif-statement

What's wrong with this WordPress code?


I want the following code to show the pagination if there are more than, or equal to, five wordpress posts on a page. I don't know how to select the post. This is basically why I need to do this: For example, If I go to a category that has 6 posts, and click on the bottom navigation to go see the last post from that category—since I only allow 5 posts per page—the nav works perfectly! BUT..if there is a category that only has 2 posts, and I allow 5, the styling for that nav still shows up. Is there an if/else statement that allows me to say, "if page displays < 5 posts, show this footer (without the posts_nav_link included) ?"

div class="wrapper">
<div class="pagination">
<?if(count($yourpostvariable)>=5){?>    
<li id="left">
      <?php posts_nav_link('','<span class="previous">&rarr;</span>','<span class="next">&larr;</span>'); ?>
   </li>
  <?};?>
</div><!-- end .pagination -->
</div><!-- end .wrapper -->

Solution

  • <?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
    
    echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    
    }
    ?>