Support » Fixing WordPress » How to get just the URI of “Next Post” not a hyperlink?

  • Hello, I am studying next_post and previous_post on the Codex page
    http://codex.wordpress.org/Template_Tags/next_post

    I use the plugin WP-print. It makes a printer-friendly alternative CSS for each single-post page. The printer-friendly version is at the URL of the post with /print/ on the end of the URL (using fancy htaccess magic).

    Now I want to add “Next Post” and “Previous Post” on my printer-friendly pages. Using the regular next_post, I get the next post’s title hyperlinked to the URL of the normal single-post page (not printer friendly). I want to instead go directly to the /print/ version of that page. Therefore I want to simply append /print/ onto the end of the next_post URL.

    So… what I want is a template tag that outputs the URL of the next post, not a hyperlinked title, not a hyperlinked word “Next >>”. Just give me the raw http:// of the next post, and let me add /print/ at the end?

    Is this possible?

    Or, another way, to strip the URL out of next_post?

Viewing 15 replies - 1 through 15 (of 16 total)
  • There’s no template tag for this, but using some custom PHP you can do what next_post() is doing to collect and display the url. Or, you can hope someone takes the idea and turns it into a plugin…

    Next-Previous Post URL plugin:
    download zip | view source

    To install download the zip file, extract next-previous-url.php, upload this to your plugins directory, and activate Next-Previous Post URL under Plugins in WordPress.

    Usage:
    <?php next_post_url(display); ?>
    <?php previous_post_url(display); ?>

    Parameter:
    display (boolean) – Display URL (TRUE), or return URL for use in PHP (FALSE). Defaults to TRUE.

    *Starts hoping for a plugin*

    waiting

    still waiting

    *blinks twice*

    wait for it

    done and done

    It works! (mostly… in my useage it’s slightly messed up when there isn’t a next post, i.e. you’re on the newest post and i have it in an a href=… I’ll figure something out. The plugin does all I could ask of it.)

    Thanks Kafka. Extraordinary response.

    It works! (mostly…)

    <?php if(next_post_url(false)) : ?>
    <a href="<?php next_post_url(); ?>">next post link</a>
    <?php endif; ?>

    Almost everything has a solution…

    While you’re on a roll, Kafkaesqui, is there a way to replicate the in_same_cat=’yes’ parameter of next_post?

    in_same_cat
    (string) Indicates whether the next post link must be in the same category/categories as current. Tag does not work in all versions of WordPress, but does work in 1.5.1.3+. If post is listed in more than one category, it will show posts within the parent category, not the child category. Options are:

    * ‘yes’
    * ‘no’ (Default)

    Can in_same_cat be replicated? Uh, yep, with a bit of work. I’ll reply when an update with this is online.

    Thanks Kafkaesqui. I appreciate the help. I owe you one (or two or three) for all the times you have helped my site.

    Ok, big changes for this ‘little’ plugin. I pulled the SQL guts and instead pass all the hard stuff to WP internals. Making it 10 times smarter, while I did no real work getting it there. 🙂

    Next-Previous Post URL 0.2 plugin:
    download zip | view source

    Usage:
    <?php next_post_url(in_same_cat, 'excluded_cats', display); ?>

    <?php previous_post_url(in_same_cat, 'excluded_cats', display); ?>

    Parameters:
    in_same_cat (boolean) [WP 1.5.1.3+]
    If next/previous post must be in the same category as the current post (TRUE), or not (FALSE). Defaults to FALSE.

    excluded_cats (string) [WP 1.5.2+]
    Numeric category ID(s) from which the next/previous post should not be listed. Separate multiple categories with an ‘and‘: '1 and 5 and 15'. There is no default.

    display (boolean)
    Display next/previous post URL (TRUE), or return URL for use in PHP (FALSE). Defaults to TRUE.

    ~Enjoy.

    Ahh, hmm. I can’t quite make this newer version work. See if I did something wrong in this implementation please:

    <?php if(previous_post_url(false)) : ?><a href="<?php previous_post_url(TRUE, '', FALSE); ?>/print/">&laquo; PRINT PREVIOUS</a><?php endif; ?> | <?php if(next_post_url(false)) : ?><a href="<?php next_post_url(TRUE, '', FALSE); ?>/print/">PRINT NEXT &raquo;</a><?php endif; ?>

    Of course I want the result to look like this:

    << PREVIOUS POST (same cat) | NEXT POST (in same cat) >>

    Note new parameter format: with 3 arguments passed, the display parameter has to be set at the 3rd argument position for your test in the if statements:

    <?php if(previous_post_url(TRUE, '', FALSE)) : ?><a href="<?php previous_post_url(TRUE); ?>/print/">&laquo; PRINT PREVIOUS</a><?php endif; ?> | <?php if(next_post_url(TRUE, '', FALSE)) : ?><a href="<?php next_post_url(TRUE); ?>/print/">PRINT NEXT &raquo;</a><?php endif; ?>

    Kafka, I haven’t gotten this (second plugin version) to work right yet, even using that last code snippit. However I need more time to mess with it, and I’m busy with exams for the next week. Just wanted to post here that I might be bumping this thread with another question next week. Thanks.

    f38ded

    (@f38ded)

    Hi,I’m new here in this wp blogging.Please tell me how to install this Next-Previous Post URL 0.2 plugin and how does it work.I have installed it and appeared in the plugins list.But when I activate it,it doesn’t appear in the option.
    What may be the problem?
    thanks

    Kafkaesqui

    (@kafkaesqui)

    f38ded, as I note above (see ‘Usage’) the plugin provides two ‘template tags’ that output the next and previous post URLs:

    <?php next_post_url(); ?>
    <?php previous_post_url(); ?>

    These would be used like normal Template Tags in WordPress, which means you insert them in your theme template, typically the single post (single.php) template. Just make sure they are placed within The Loop.

    f38ded

    (@f38ded)

    Hi kafka,thanks for your support but your link “The Loop” above is not working.I’m using the theme “Andreas04 2.0 by Tara Aukerman”.Please tell me the codes in single.php in between which I should put the previous/next code given above.
    thanks,
    cheers,

    Kafkaesqui

    (@kafkaesqui)

    The Loop is presently working for me, but if the Codex is down (and it often is these days), here’s the Google cache for that page.

    f38ded

    (@f38ded)

    kafka,I’m beginner here and not familiar with this codes.Can you tell me the exact codes in single.php of “Andreas04 2.0 by Tara Aukerman” in between which I can put the next/previous codes above..
    I want to display like << PREVIOUS POST | NEXT POST >> at the end of page.
    thanks

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘How to get just the URI of “Next Post” not a hyperlink?’ is closed to new replies.