• Hello!

    I’ve been trying for days to write out an original plugin that will show “previous” and “Next” links that will direct to the next post alphabetically by title.

    For instance, If I posted these:

    Title: Hello my name is | page:/?p=50
    Title: Hello my surname is | :/?p=15
    Title: Hello my dog’s name is | page:/?p=25
    Title: Hello my Workplace name is | page:/?p=23

    The it would go in the following order:
    dog > name > surname > workplace

    Regardless of post id. Just based on the title.

    Sadly I have done nothing except create errors lol.

    I have also tried editing existing previous/next plugins but still no joy.

    Please help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jirikai

    (@jirikai)

    Can anyone help?

    Hey Jirikai,
    I had a similar need and ended up discovering that this really needs a custom query to make it happen. I’ve never made a plugin, so I’m sure this could be plugin-ized, but I didn’t pursue that.

    This looks at a value passed in the URL and saved to the variable $val to see if this is a “list” or “thumbnail” view. If it’s a list, then the next link is the next one alphabetically, if it’s a thumbnail, then the next link is the previous post chronologically.

    There’s a little extra complexity here b/c it’s also limiting them to be within a single category.

    <?php
    	$category = 'projects'; //limit results only to projects
    	$currentPostTitle = $wpdb->escape($post->post_title); //get the Title of the current post, escape it b/c some titles have special characters
    	$currentPostDate = $post->post_date; //get the date of the current post
    	if ($val == "list") { $threshold = "AND p.post_title > '$currentPostTitle'"; $sortByNext = "p.post_title ASC" ;} else { $threshold = "AND p.post_date < '$currentPostDate'"; $sortByNext = "p.post_date DESC" ;} // evaluate view value and set sort order depending on alpha (list) or chronological (thumb)
    	$querystr = "
    					SELECT p.* from $wpdb->posts p, $wpdb->terms t, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t2, $wpdb->term_taxonomy tt2, $wpdb->term_relationships tr2
    					WHERE p.id = tr.object_id
    					AND t.term_id = tt.term_id
    					AND tr.term_taxonomy_id = tt.term_taxonomy_id
    					AND p.id = tr2.object_id
    					AND t2.term_id = tt2.term_id
    					AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
    					AND (tt.taxonomy = 'category' AND tt.term_id = t.term_id AND t.slug = '$category')
    					AND p.post_status = 'publish'
    					$threshold
    					ORDER BY $sortByNext
    					LIMIT 1
    					";
    	$pagePostsNext = $wpdb->get_results($querystr, OBJECT);
    ?>
    <?php if ($pagePostsNext): ?>
    	<?php foreach ($pagePostsNext as $postNext): ?>
          <a href="<?php bloginfo('url'); ?>/<?php echo $postNext->post_name; ?>?<?php if (isset($val)) echo 'view=' . $val; ?><?php if (isset($keyword)) echo '&amp;keyword=' . $keyword; ?><?php if (isset($side)) echo "&amp;side=" . "$side";?>">Next project</a>
      <?php endforeach; ?>
    <?php else : ?>
    <?php endif; ?>

    Thread Starter jirikai

    (@jirikai)

    I’m lost.

    I took a look at your code.
    Changed the category to my own and checked to see if it would work. While no PHP errors occured, nothing appeared either.

    if ($val == “list”)

    What is this? How do i set the value to list? How does it decide on a value?

    Thank you for your help so far ^^

    I’m also interested in this. I’ve been searching for a couple hours and couldn’t find anything.

    Thread Starter jirikai

    (@jirikai)

    ?? Seems I’m not the only one hunting for this function.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Alphabetical previous/next links?’ is closed to new replies.