WordPress.org

Ready to get started?Download WordPress

Forums

How to get just the URI of "Next Post" not a hyperlink? (17 posts)

  1. Dgold
    Member
    Posted 8 years ago #

    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?

  2. Kafkaesqui

    Posted 8 years ago #

    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.

  3. Dgold
    Member
    Posted 8 years ago #

    *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.

  4. Kafkaesqui

    Posted 8 years ago #

    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...

  5. Dgold
    Member
    Posted 8 years ago #

    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)

  6. Kafkaesqui

    Posted 8 years ago #

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

  7. Dgold
    Member
    Posted 8 years ago #

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

  8. Kafkaesqui

    Posted 8 years ago #

    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.

  9. Dgold
    Member
    Posted 8 years ago #

    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) >>

  10. Kafkaesqui

    Posted 8 years ago #

    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; ?>

  11. Dgold
    Member
    Posted 8 years ago #

    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.

  12. f38ded
    Member
    Posted 7 years ago #

    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

  13. Kafkaesqui

    Posted 7 years ago #

    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.

  14. f38ded
    Member
    Posted 7 years ago #

    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,

  15. Kafkaesqui

    Posted 7 years ago #

    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.

  16. f38ded
    Member
    Posted 7 years ago #

    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

  17. Kafkaesqui

    Posted 7 years ago #

    (For some of us, having to download themes to clarify an answer can get tiring. That's not a slap at you f38ded -- just wanted to vent a bit.)

    In Andreas04 it appears the post resides in this div:

    <div class="entry">

    So anywhere in there should be fine. Just note that a post's *functionality* begins at this point:

    the_post();

    Any 'in The Loop' template tags or functions should occur after that appears in a template.

    Finally, be aware my plugin merely outputs the url for the next and previous posts. It's possible the built-in template tags may better fit your needs (though I've no idea what you're planning, so I could be wrong):

    http://codex.wordpress.org/Template_Tags/next_post_link
    http://codex.wordpress.org/Template_Tags/previous_post_link

Topic Closed

This topic has been closed to new replies.

About this Topic