• Resolved Vaughan

    (@vaughan-van-dyk)


    Hi there,

    Something that’s been asked a few times before but never truly resolved is how to get just the URL/permalink of the next and previous posts when on a current post. I need to feed these as variables into a Flash movie.

    The next_post_link and previous_post_link tags unfortunately have the added contents of a href=”…” so I’d need to use PHP substring commands to strip those out, which I’d prefer not to have to do each time.

    I could use the get_previous_post and get_next_post tags, for example:

    <?php
      $prevPost = get_previous_post('','','','yes');
      $prevURL = get_permalink($prevPost->ID);
    ?>

    …but these tags are deprecated and it appears that there is still a problem if you want them to get posts only from the same category (at the moment they get posts from next and previous category too, even when in_same_cat is set to ‘yes’ like above).

    There must be a way to get just the URLs, without needing a plugin or hacking WordPress back-end code.

    Thanks for all the help.
    Vaughan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Vaughan

    (@vaughan-van-dyk)

    In the meantime, I managed to figure out what was going wrong here (working with too many examples at once :).

    The tags that have been deprecated are actually previous_post and next_post, not get_previous_post and get_next_post. The latter two also do not have the same syntax as previous_post, next_post, previous_post_link, nor next_post_link.

    Looking at the link-template.php file, you’ll notice they each take two parameters, both of which are optional. It’s the first parameter in that case that specifies whether the posts should be in the same category (and hence why my preceding example wasn’t working).

    So updated code for this is as follows:

    <?php
      $prevPost = get_previous_post(true);
      $prevURL = get_permalink($prevPost->ID);
    ?>

    Hope that helps anyone else who had a similar request.

    Vaughan

    hmm. i hate how WP have to spit everything out automagiacally. i found the same solution as you thou’… but there’s a huge problem. it can’t exclude several categories… i’m going crazy..
    and as you say in link-template.php it says:

    function get_previous_post($in_same_cat = false, $excluded_categories = '') {
    	return get_adjacent_post("$in_same_cat", $excluded_categories);
    }

    two parameteres.. but refers to another function.. aaaaaaaaaaa. while reading that function. cats are exploded not by comma but by “and”… crazy inconcistent writing..

    ok.. so here’s the nice solution for excluding categories:

    <?php
      $exclude_cats = "5 and 8 and 12"; // example - yes, use quotes!
    
      $prevPost = get_previous_post(false, $exclude_cats);
      if($prevPost != '') { $prevURL = get_permalink($prevPost->ID); }
    
      $nextPost = get_next_post(false, $exclude_cats);
      if($prevPost != '') { $nextURL = get_permalink($nextPost->ID); }
    ?>

    remember if parameter inside get_permalink is blank then it will get link for current post, that’s why you should have that if-statement.
    first parameter in get_next_post / get_previous_post is if browsing the same category. Second is that exclusion list seperated by ‘and’, and with use of “quotes”.

    happy coding

    Hej,

    I’ve been trying to use it like this,

    <a href="<?php $prevPost = get_previous_post(true);
    	  $prevURL = get_permalink($prevPost->ID); ?>">previous

    but it just gives me a link to the current post, if i’m on ?p=6 it generates a link to ?p=6,

    what am i missing?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting URL of next/previous links’ is closed to new replies.