WordPress.org

Ready to get started?Download WordPress

Forums

How to call next_post and previous_post only alphabetical? (14 posts)

  1. Dgold
    Member
    Posted 7 years ago #

    How do I make previous_post() except in alphabetical order not chronological order?

    How can I do this, using previous_post or another technique of PHP to call the "next post in alphabetical order by title"?

  2. You'll have to code something custom (you can probably use the existing functions as a base) or look for a plugin. Those functions will not do that.

  3. Dgold
    Member
    Posted 7 years ago #

    I'm hoping someone more learned in the ways of PHP will write the code for me :)

    I understand I can't use previous_post and next_post, because these functions doesn't support a parameter of order=alphabetical. I figure it's only a few lines of PHP, but I have no idea how to write it. Could you help?

  4. Sorry, not sure how to do it easily. :/

  5. Dgold
    Member
    Posted 7 years ago #

    Would it be a Loop?

    It seems like someone could write this code easily. I've already got a Page that lists all my posts in Alphabetical order. Now I just need the Single Post page to know what the next Post in alphabetical order is titled.

    Here's my custom query that shows an index of all the posts in alphabetical order:

    <?php query_posts('orderby=title&order=asc&showposts=-1'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><<a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?><?php endif; ?>

    Does that help-you help me?

  6. Dgold
    Member
    Posted 7 years ago #

    Anyone got another idea?

    This seems like it should be simple...

    Vkryl, Kafkaesqui, Geoff, Miklb, anyone else that can help?

  7. Dgold
    Member
    Posted 7 years ago #

    Sorry to BUMP

    One more request for help on this... Anyone can code it?

    If not maybe I will rephrase the question and post again. I'm convinced this isn't difficult for someone who knows how to write queries. I don't know how yet. I'll do you a favor in return as some kind of payback!

  8. Kafkaesqui

    Posted 7 years ago #

    No need to rephrase. I just never saw the thread...

    Next-Previous Post by Title plugin:
    Download plugin | View source

    Alphabetical ordering is of the simplest kind, where A = a, etc. Anyway, since I merely swiped WP's code and rewrote it, the template tags:

    <?php next_post_title(); ?>
    <?php previous_post_title(); ?>

    use the same arguments as next_post_link() and previous_post_link(). So to customize the functionality of the tags, just read up on the parameter info from those pages. ;)

  9. Dgold
    Member
    Posted 7 years ago #

    A+ response! Thanks for solving this request, Kafkaesqui. Very nice of you to make a plugin. I hope this suits someone else's needs too (can't believe I am the first to need this? Don't people make cookbooks, poetry collections, movie reviews, glossaries, FAQ's, etc. with WordPress? Alphabetical is useful!)

    I've discovered one place where this plugin is tripped-up, in my particular case study.

    On this series of Post Titles, for some reason it gets stuck in a loop between the slugs im-not-fit and im-waiting-for-the-man, such that it will not recognize the next post title should be ive-been-seeking. It sends me backwards at that point using next_post_title, from im-waiting back to im-not, instead of forward to ive-been. Any idea why? Not a deal-breaker though, still a good solution for most of my posts.

  10. Kafkaesqui

    Posted 7 years ago #

    "for some reason it gets stuck in a loop between the slugs im-not-fit and im-waiting-for-the-man"

    I did *some* testing of the tags and didn't see a problem like this one. However, note the plugin is not ordering on post name, but post title. So there's possibly some hang up with apostrophes (but don't quote me).

    I'll look at the problem and repost if I figure out a fix.

  11. Dgold
    Member
    Posted 7 years ago #

    It messes up in a couple spots, seemingly related to apostrophes.

    Where I typed normal apostrophe in the post title field, WP converted them to smart/fancy apostrophes... and simply omits the apostrophe in the post-slug. All that could have some effect?

    This is a helpful plugin & much appreciated.

  12. Kafkaesqui

    Posted 7 years ago #

    "Where I typed normal apostrophe in the post title field, WP converted them to smart/fancy apostrophes... and simply omits the apostrophe in the post-slug. All that could have some effect?"

    I wish that were the case, but the plugin is touching the titles outside of WordPress' formatting.

    Eh, I'll figure it out... ;)

  13. Kafkaesqui

    Posted 7 years ago #

    Ok, I think I wrapped my head around what I was doing wrong in the plugin's query. Release 1.0.1 should take care of the looping on post titles with single quotes. Same links above.

    Also slipped in support for the next update to WordPress, so I won't have to deal with it later...

  14. Dgold
    Member
    Posted 7 years ago #

    Outstanding! That fixed it. Works perfectly for me now. Thanks & I hope this is useful to someone else out there as well!

    It would be great for anyone making a glossary/dictionary or anything alphabetical using WP.

Topic Closed

This topic has been closed to new replies.

About this Topic