WordPress.org

Ready to get started?Download WordPress

Forums

Smarter Navigation
[resolved] Using next_post_smart() for complex next/previous links (4 posts)

  1. Parakoos
    Member
    Posted 2 years ago #

    Sometimes, a simple textual link to the next/previous links doesn't cut it. I, for example, bring back the post IDs of the next/previous posts and construct nice links using the post thumbnail.

    You can do this with Smart Navigation using a, for reasons unknown, undocumented method called get_adjacent_id_smart(). Here is an example from my single.php:

    $tmn_next_post = get_post(get_adjacent_id_smart( false ));
    $tmn_previous_post = get_post(get_adjacent_id_smart( true ));

    You can see a working example of this on The Modern Nomad.

    http://wordpress.org/extend/plugins/smarter-navigation/

  2. Parakoos
    Member
    Posted 2 years ago #

    Actually, there is an added complication with the above. For some reason, if there is no next or previous post, the get_adjacent_id_smart() method returns the current post! So to fix this, you need something like this:

    $tmn_next_post = get_post(get_adjacent_id_smart( false ));
    $tmn_previous_post = get_post(get_adjacent_id_smart( true ));
    if( $tmn_next_post->ID == get_the_ID() ) { $tmn_next_post = false; }
    if( $tmn_previous_post->ID == get_the_ID() ) { $tmn_previous_post = false; }
  3. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    if there is no next or previous post, the get_adjacent_id_smart() method returns the current post!

    That is not accurate. When there's no post, get_adjacent_id_smart() returns false. But, when you pass false to get_post(), it returns the current post.

    Either way, thanks for taking the time to share your knowledge of the plugin.

  4. Parakoos
    Member
    Posted 2 years ago #

    Ahhh. Got it. My bad.

    And thanks for an awesome plugin!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic