scriptygoddess

24 Jul, 2004

Next/Previous posts in same category

Posted by: Jennifer In: WordPress Plugins

UPDATE 12/4/2007 PLEASE NOTE – this plugin is now broken in the most current version of WordPress (which is, as of this writing, ver 2.3.1). However – it is also no longer needed as this functionality is now part of WordPress. Please see this post for more details. I will leave this post up here for the sake of archives, etc. but it is no longer current.

I've seen a few people request the ability to do next/previous posts within the same category. I thought it had already been done and I swear I looked around to see if anyone had, but I didn't find it. If it has been done, well at least I had fun writing this. :)

Current Version: v 0.2
Last updated: 10/04/04 8:51pm

So here's my next/previous post in category plugin:

1) Download and install this plugin (right-click save as.. with a .php extension) – upload to your wordpress plugins folder (wp-content/plugins/ ). Activate through the admin interface.
IMPORTANT NOTE ABOUT DOWNLOAD: Make sure you do NOT have any extra spaces after the last ?> in your copy of the file. Extra spaces will give you various "header already sent" type errors.

2) This plugin has two functions:

previous_cat_post();
next_cat_post();

To use the plugin functions you can simply call them where you want the next and previous links displayed (but they must be called from within the wp loop). The default display will look like this:

Of course you can customize it.

3) Each function has the following parameters you can pass to it:

$beforeGroup (string) text/html to be displayed before the list of categories and posts. Default is '<ul>'.

$afterGroup (string) text/html to be displayed after the list of categories and posts. Default is '</ul>'.

$beforeEach (string) text/html to be displayed before each category/post. Default is '<li>'.

$afterEach (string) text/html to be displayed after each category/posts Default is '</li>'.

$showtitle (true | false) true if you want to include the title of the post in the link (see below for how this looks if set to false). Default is true.

$textForEach (string) text/html to be displayed with each line. If $showtitle is set to false – this line becomes the link. If $showtitle is true – this line will be displayed before the title (which will be link). This differs from $beforeEach in that you can have it dynamically display the category name. Where you want the category name to display – put a %. Default for previous_cat_post is 'Previous post in %:<br />'. Default for next_cat_post is 'Next post in %:<br />'.

So some sample uses:

previous_cat_post($beforeGroup='<p>Previous Posts within categories</p>', $afterGroup=", $beforeEach='<p>- ', $afterEach='</p>', $showtitle=false, $textForEach='Previous post in %');

Would display this:

Previous Posts within categories

Previous post in CategoryName

Previous post in Different CategoryName

(notice that here, since we set $showtitle to false, the $textForEach becomes the link without showing the title of the post.)

Change Log
New in ver 0.2
Links that are generated now make use of the customized permalink structure.
Supports up2date plugin.

New in ver 0.1.2
Fixed a minor bug relating to double-quotes in titles.

40 Responses to "Next/Previous posts in same category"

1 | zuppah » scriptygoddess: next/previous post nella stessa categoria

July 25th, 2004 at 6:28 am

Avatar

[…] rie: software: wordpress links » zuppah @ 8:21 am , letto 1 volte

eccolo qui! vi riporto qui il post di jennifer: […]

3 | the2nd : blog and attitudes » My WPplugin I

September 8th, 2004 at 6:42 am

Avatar

[…] yang dari WordPress itu Next-Previousnya dari seluruh kategori. Plugin ini bisa didapet di sini Page Navigation Kalo di wordpress, navigasi halaman […]

4 | Accesibilidad en la red » Sistema de navegación y elementos orientativos

November 16th, 2004 at 8:05 pm

Avatar

[…] xisten otros plugins, bastante complementarios a este, para la navegación por fechas y por categorías. En cuanto a los "breadcrumbs" para […]

5 | zuppah

July 25th, 2004 at 2:28 am

Avatar

scriptygoddess: next/previous post nella stessa categoria
eccolo qui!
vi riporto qui il post di jennifer:

1) Download and install this plugin (right-click save as.. with a .php extension) – upload to your wordpress plugins folder (wp-content/plugins/ ). Activate through the admin interface.
IMPORTANT…

6 | BB

July 25th, 2004 at 3:39 am

Avatar

Can this be used like the Previous Archive Date / Next Archive Date links, only appearing once a user has chosen a category of posts to display? If possible, that would integrate nicely into the overall navigational scheme.

Ideally, the same navigation look and feel would be nice on the archive, category, and index pages.

7 | Amit Gupta

July 25th, 2004 at 7:12 pm

Avatar

I've also seen its implementation on many Blogs but don't recall anyone sharing the code. Or maybe my memory is just leaky. 😉

Thanks for it!! 😀

8 | paticoflange

July 29th, 2004 at 1:53 pm

Avatar

Hi. Very nice plugin, thank you. And easy to implement.

Functionality question: currently the "next" "previous" links show up all the time. Is there a way to make them only show when you're viewing one post at a time?

10 | Bryan

July 30th, 2004 at 12:15 am

Avatar

Very nice plugin but I seem to have some problems with it in the admin pages;

Warning: Cannot modify header information – headers already sent by (output started at [some-path]/wp-content/plugins/nextPreviousCat.php:110) in [some-path]/wp-admin/post.php on line 340

This error occurs when I make changes to on the category page. Line 110 in the plugin is the closing ?>

11 | Jennifer

July 30th, 2004 at 7:38 am

Avatar

Bryan – read step 1 again:
IMPORTANT NOTE ABOUT DOWNLOAD: Make sure you do NOT have any extra spaces after the last ?> in your copy of the file. Extra spaces will give you various "header already sent" type errors.

12 | Bryan

July 30th, 2004 at 10:06 am

Avatar

"Bryan – read step 1 again:"

More like "actually read step 1 before you post stupid comments." 😉

I think I got as far as "Download and install th–*" and left the rest to people who read instructions.

Thanks for the help.

13 | Andrew

August 4th, 2004 at 11:07 pm

Avatar

For my purposes, this really needed to be next/previous not by id, but by date, and this can happen by just changing four lines: first, in addition to getting the currentPostId, also get currentPostDate ($post->post_date), in the two functions; second, in the two functions, where the query is, instead of comparing currentPostId against the post_id, compare currentPostDate against the post_date.

This way if your post ids aren't in order because you've had lots of drafts lying around that you've only managed to get around to completing here or there in no order, then this minor change can be quite useful.

14 | Rob

October 2nd, 2004 at 5:55 pm

Avatar

Thanks for the plugin – adds a great feature to wordpress.

I am using permalinks that are similar to yours in this site, but have noticed that in my implementation of your plugin the permalinks are not used in the next and previous links – instead the normal …/?P=22 etc links are showing up.

Has anyone else had this issue? Is there a known cause, or am I just being dumb?

15 | Jennifer

October 2nd, 2004 at 9:30 pm

Avatar

Rob – I wouldn't say it was an "issue" – more like "as designed". I guess if it was something you (or people in general) really needed, I could probably continue working on the plugin and get it to follow the permalink structure set in options.

16 | Rob

October 3rd, 2004 at 6:22 am

Avatar

Permalinks implementation would be a feature that I would appreciate, as would anyone else who uses permalinks. If you mix permalink and non-permalink links on the same site, then it messes up the search engines – Google finds the same page twice, with different URL's – one with the permalink and one without. This messes up the page rankings.

Is there anything I (or someone else to whom this is important) can do to help?

17 | Jennifer

October 4th, 2004 at 9:02 pm

Avatar

Plugin updated to ver 0.2. See updates in post for details.

18 | Rob

October 5th, 2004 at 4:36 am

Avatar

Great! Works Perfectly!

19 | marc

October 6th, 2004 at 7:35 pm

Avatar

I have attempted to implament this code using the following code but receive parse errors, what is the correct code to use?

<?php if ($single) { previous_cat_post();
next_cat_post(); } ?>
<?php } ?>

20 | Jennifer

October 6th, 2004 at 8:00 pm

Avatar

whats the extra
<?php } ?>
at the end there for? You already have a } closing the if statement above… Not sure if that was closing another statement (not shown in your snippet)

21 | marc

October 7th, 2004 at 9:04 am

Avatar

Jennifer I didn't catch the extra closing. I have tries the following code with no sucess, it will parse but shows no effect on the cat pages.

<?php if ($single) { ?> <?php previous_cat_post('% |',",'yes') ?> <a href="<?php bloginfo('url'); ?>">Main</a> <?php next_cat_post('| %',",'yes') ?> <?php } ?>

This is the exact code for individual pages with the exception of the subsitution for use for cat pages.

Obviously it is wrong. What is the exact code to display next cat/main/previous cat?

22 | Jennifer

October 7th, 2004 at 12:56 pm

Avatar

I haven't tested this – but give this a try. I think the problem is that you weren't passing the correct arguments:

<?php
if ($single) { previous_cat_post($beforeGroup=", $afterGroup=", $beforeEach=", $afterEach=' ', $showtitle=false, $textForEach='%|');
?>
<a href="<?php bloginfo('url'); ?>">Main</a>
<?php
next_cat_post($beforeGroup=", $afterGroup=", $beforeEach=", $afterEach=' ', $showtitle=true, $textForEach='| %');
}
?>

23 | Jennifer

October 7th, 2004 at 12:57 pm

Avatar

Woops – make that $showtitle=true (not false) in previous_cat_post()

24 | Jennifer

October 7th, 2004 at 1:11 pm

Avatar

Ok, let's try this again… This I DID test and know it works:

<?php
if ($single) {
previous_cat_post($beforeGroup='~', $afterGroup=' | ', $beforeEach=", $afterEach=' ', $showtitle=true, $textForEach=");
?>
<a href="<?php bloginfo('url'); ?>">Main</a>
<?php
next_cat_post($beforeGroup=' | ', $afterGroup='~', $beforeEach=", $afterEach=' ', $showtitle=true, $textForEach=");
}
?>

25 | Jennifer

October 7th, 2004 at 1:14 pm

Avatar

I had meant to put in a << and >> characters instead of those ~ (woops again. Sorry)

26 | Jeremy

October 13th, 2004 at 2:32 pm

Avatar

Fine plug-in. Thanks. I have two questions:

If I want to put the Next and Previous links inside a CSS class, so that I can align them right and left for example, where would I do that? In the beforeGroup and afterGroup, or some other place?

I'd also like these links to display only for one category, but I can't seem to get the syntax of the PHP if statement right. Any help would be most welcome.

Thanks

Jeremy

27 | Kenny Youens

October 28th, 2004 at 1:40 am

Avatar

You know, this function used to be built in to WordPress. And supposedly, according to the documentation, it still is: the next_post and previous_post functions have a parameter that limits the results to the category of the current post in the loop. Unfortunately, the functions rely on $post->$post_category which is deprecated in newer versions of WP. The functions are broken as a result. So, this is a useful plugin, Perhaps it could be incorporated into the main distribution?

28 | metoo

March 30th, 2005 at 5:25 pm

Avatar

Feature request: could you please add another macro which displays the topic of the next post?

for example:

Next post in category SciFi: Andromeda!

29 | echo

April 19th, 2005 at 3:17 am

Avatar

Thanks scriptygoddess! This plugin does just what we needed, except we also need it to cycle through the posts by date, not by ID.

In comment #8 above, Andrew suggested the code to edit the plugin to do this. Has this been tested? If this is possible, I would much appreciate instructions for editing which code to what. Thanks again for this much needed plugin!

30 | Andrew

April 19th, 2005 at 11:01 pm

Avatar

As regards the comment 23 by "metoo" do you mean topic in some manner other than post title?

As regards comment 24, I, Andrew, have done the changes that I suggested, but since no one else said anything about my comment, I didn't think anyone else cared about that functionality.

I'll be happy to e-mail you my modified version, but I'd so much more appreciate it first if Jennifer would tell me if she'd like to incorporate that change into her official distribution or not – I hate to "fork" good code without good reason – if so, I'll e-mail her my modified version, and she can announce the change as she sees fit.

31 | Jennifer

April 20th, 2005 at 7:31 am

Avatar

Andrew – Always appreciate the help!! Especially now while I'm too busy to update this stuff. (But that's changing!!! I'm working on it… just a few more weeks!!) 😀 – Anyway – yes, feel free to email me your code, and I'll try to square away some time to get it incorporated into the script! email: scripty at scriptygoddess dot com

32 | echo

April 20th, 2005 at 4:24 pm

Avatar

Andrew + Jennifer, Great news on code to cycle through posts by date. I await the update.

This has just been addressed by the WP developers (nav constrains to current category)
http://mosquito.wordpress.org/view.php?id=1252
although just replacing the updated file (plus editing theme file) broke the output.

33 | zota

May 9th, 2005 at 3:07 pm

Avatar

Jennifer + Andrew,

I'm trying to hack this plugin to order by date, but judging by the MySQL syntax errors I'm getting, it's beyond me…

If you could post the adjusted code somewhere, I'd be very grateful.

34 | echo

May 22nd, 2005 at 4:56 am

Avatar

WordPress v1.5.1 has re-enabled next_post() and previous_post() to honor "same_category" restriction.

35 | Escrita Torta em Linha Reta » Blog Archive » Top Plugins para WordPress

December 29th, 2006 at 5:47 am

Avatar

[…] Next/Previous Posts in same Category: Como o nome já diz, apresenta uma janela de navegação onde mostra quais são os artigos anterior e posterior da mesma categoria do artigo selecionado. […]

36 | My WordPress Plugins

January 13th, 2007 at 12:27 pm

Avatar

[…] Next/Previous Post in same Category by Jennifer – Scriptygoddess This plugin will give you two functions that will give you links to the next/previous post in the same category as the current post. […]

38 | 葱白·后宫 » Blog Archive » 常用代码

July 15th, 2007 at 1:11 am

Avatar

[…] 同类别中的前一篇及后一篇文章(Next/Previous Post in same Category) […]

39 | scriptygoddess » Blog Archive » Next Previous Posts in Same Category

December 4th, 2007 at 1:34 pm

Avatar

[…] gotten a few emails about my (old) Next/Previous Post in Same Category plugin and about the fact that's its broken – however, this functionality is now built into […]

40 | The Italian blog is born: reasons and technicalities « P.S.

March 2nd, 2009 at 2:47 am

Avatar

[…] course I changed both blogs. I also installed Next/Previous Post in same Category, which worked perfectly, and used it to build the two little boxes that you see on the post page, […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements