0
votes

i want add next post and previous post link my each post. i already do it. but i want when last post that time next post link will hide and first post time previous post will be hide

bellow my code is working ...

 <?php
     $prev_post = get_previous_post();
     if (!empty( $prev_post )): ?>
      <a href="<?php echo get_permalink( $prev_post->ID ); ?>" title="<?php echo $prev_post->post_title; ?>" class="prev-post">
      <span class="left_arrow"></span> <?php _e("Previous Post", "twentytwelve"); ?></a>
    <?php endif; ?>


    <?php
    $next_post = get_next_post();
    if (!empty( $next_post )): ?>
    <a href="<?php echo get_permalink( $next_post->ID ); ?>" title="<?php echo $next_post->post_title; ?>" class="next-post">
    <?php _e("Next Post", "twentytwelve"); ?>&nbsp; <span class="right_arrow"></span></a>
    <?php endif; ?>

bellow one suppose to work to but not.

<?php if($show_prevnext_post == 1) { ?>

    <?php
     $prev_post = get_previous_post();
     if (!empty( $prev_post )): ?>
      <a href="<?php echo get_permalink( $prev_post->ID ); ?>" title="<?php echo $prev_post->post_title; ?>" class="prev-post">
      <span class="left_arrow"></span> <?php _e("Previous Post", "twentytwelve"); ?></a>
    <?php endif; ?>



    <?php
    $next_post = get_next_post();
    if (!empty( $next_post )): ?>
    <a href="<?php echo get_permalink( $next_post->ID ); ?>" title="<?php echo $next_post->post_title; ?>" class="next-post">
    <?php _e("Next Post", "twentytwelve"); ?>&nbsp; <span class="right_arrow"></span></a>
    <?php endif; ?>


    <?php } ?>

i don't understan why this is not working. should i need to anything in function.php

1
is $show_prevnext_post returing value 1 - sAnS
if i use <?php if($show_prevnext_post == 1) { ?> .. then my next previous even not coming. - Accore LTD
from where you getting the value for $show_prevnext_post and what value you passing.. - sAnS
it was copied from another theme. i just see that have function in another page. and not so easy to use i guess.. but same what i wanted. i can able at last make it.. just simpley useing @sarahm link. .... - Accore LTD
thats good you able to solve but you should know what you are doing before asking others.. - sAnS

1 Answers

1
votes

Take a look at WordPress's previous_post_link() and next_post_link() functions. They will only output a link if there's a prev/next post. But it will only work if you use them in the WordPress Post Loop

Hope that helps :-)