• So I currently have links which go to previous and next post but I’m trying to build something a bit more complex.

    Let’s say I have two parents post and each one of these have at least one or more child posts.

    So if I’m in parent post one the next link should take to the next parent post and viceversa(previous parent post), now what I would like to do is if I’m in a child post the link should take to its siblings(next child post and previous child post) not the parent ones.

    I currently have this html which in in fact copied from the WordPress codex.

    
    <!-- Previous and Next Post -->
    <?php if(is_single()) : ?>
    <div class="btn-group btn-group-justified hidden-sm hidden-xs" role="group" aria-label="..." id="nextpreviouslinks">
      <div class="btn-group" role="group">
        <button type="button" class="btn btn-default btn-sm"> << <?php previous_post_link( '%link', '%title'); ?></button>
      </div>
      <div class="btn-group" role="group">
        <button type="button" class="btn btn-default btn-sm"><a href="<?php echo get_permalink( $post->post_parent ); ?>"><?php echo get_the_title( $post->post_parent ); ?></a></button>
      </div>
      <div class="btn-group" role="group">
        <button type="button" class="btn btn-default btn-sm"> >> <?php next_post_link( '%link', '%title' ); ?></button>
      </div>
    </div>
    <?php endif; ?>
    <!-- /Previous and Next Post -->
    

    I have tried to look for function online that can do the work but I just don’t find any.

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Hi kirasiris,

    Yes, I’ve not ever seen any such function either. It’s apparently something you’d need to custom code. The next/previous post link functions at their heart rely upon get_adjacent_post(). Your task would be to use this function’s code as a base upon which your needed logic is applied. You should be able to see that this function queries for the next/previous post based on date. Where applicable the query is further restricted by assigned terms and post status. If you don’t need these extra features, sizable chunks of code will not be needed.

    Backing up to a post’s parent will not require a query since the parent ID is part of the post object. Anything else will require a query, constrained as required by whether posts are top level parents or not. Once you have the right post based on your criteria, it’s straight forward to determine its link and output it in an anchor tag.

Viewing 1 replies (of 1 total)
  • The topic ‘Previous and Next Link’ is closed to new replies.