WordPress.org

Ready to get started?Download WordPress

Forums

next_post_link/previous_post_link (6 posts)

  1. YellowSwordfish
    Member
    Posted 9 years ago #

    Can anyone help me with some php here using these tags (and I gather they are new and not yet documented in the codex)

    I want to determine if there IS a next or previous post in the navigation. Because the functions echo - any test I perform automatically displays the content which makes it hard to test to see if anything is being returned. is there any other test I can perform that if TRUE then calls one of these tags?

    Thanks

  2. skippy
    Member
    Posted 9 years ago #

    next_post_link() and previous_post_link() are defined in /wp-includes/template-functions-links.php. The very first line of next_post_link() is:
    $post = get_next_post($in_same_cat, $excluded_categories);

    This is a common theme with WordPress template tags / functions: there are two functions that do the same thing. One returns the result(s) and the other outputs the result(s). The returning functions are usually named get_*.
    [Note: there are a few exceptions to this, but not many.]

    So, if you do:
    $post = get_next_post();
    if ($post) { // do stuff }

    you should be set.

  3. YellowSwordfish
    Member
    Posted 9 years ago #

    skippy - you're a marvel. Now I know this snippet of structure it might resolve a few other tweaks.
    Thanks very much - I'll try it right away.

  4. YellowSwordfish
    Member
    Posted 9 years ago #

    Like a charm.
    Thanks very much

  5. YellowSwordfish
    Member
    Posted 9 years ago #

    Actually... yes it does work.
    But then I notice that the Archive page uses a variant. next_posts_link which is a different routine and doesn't appear to have the same option as you gave me for the single page query. Can you by any chance offer a solution for this one? I would have liked to style it the same way!
    Thanks

  6. YellowSwordfish
    Member
    Posted 9 years ago #

    While on the subject, the 'plural' version s used in Archive and Search templates are phrased the opposite way to the single page version. I.e., the 'Next' function actually reteurns 'previous' posts as far as date order goes...

Topic Closed

This topic has been closed to new replies.

About this Topic