This is SameCat, the a very, very simple hack/plugin for WordPress that outputs a list of posts in a given category or set of categories – good for listing a few posts ’similar’ to the current post being output in the main $posts loop.
NEW VERSION!
Please grab the , and use the new version of CG-Amazon it has!
NEW VERSION!
- Updated 04.20.04, updated one possible fall-through case where the output might not end up with an item (randomizer must be going wrong somehow) and not validate. Still looking for any issues. Validated against 1.2.
- Updated 05.20.04, added ability to pass in a single category ID to match, rather than results of get_the_category().
- Updated 06.11.04, made it compatible as a WP1.2 Plugin. Though you can still direct-include it in my-hacks if you really, really must…

- Updated 06.12.04, fixed one bug with the new plugin format, improved documentation, added orderby for non-random results ordering.
- Updated 06.13.04, major cleanup, html-based documentation.
32 Comments
RSS feed for comments on this post.
The URI to TrackBack this entry is: http://www.chait.net/wp-trackback.php/87




heya,
i’m using your great hack on www.pixelblog.de and updated to wordpress 1.2 earlier this week (nightly builds).
now i have the problem with sub-categories not be shown.
can you workaround that or release a new version?
sorry to bother but the hack is just great, i don’t want to dismiss ‘m
I’ll have to look into it. I’m still on the 1.0 base, as a lot of the newer features weren’t going to be that useful for me. I’ll probably be upgrading over the next few weeks anyway, so I’ll poke around in the next few days on this.
FYI, I read elsewhere that
$tablepostsis deprecated and won’t work with 1.3 or greater.Great hack! I’m going to see if I can put this to work.
Does this work as a “plugin” in WP 1.2? How do I use it with 1.2? I really need this…
Just quickly updated it to be a 1.2 compliant plugin.
Unfortunately, the plugin isn’t working for me. The string:
php echo list_posts_of_cat(get_the_category(), 25, 0);
Just results in “No matching posts.” even though there are matches. The my-hacks version still works fine.
I don’t know how much has been changed since the my-hacks version, but I’d like to see two things changed about how this works:
(1) I’d like it to exclude the current page from the list.
and
(2) I’d like the posts to be listed alphabetically, by title.
Cheers.
The comments RSS feed for this post doesn’t seem to work for me…
Fixes are in, try the latest. Looking into problems with RSS comment feeds…
In 1.2 change:
to:
to get the look here - generositywithoutborders.org
(this also required the fix of placing the missing ‘.$before.’ command in template-functions-general.php line so it says (when fixed):
return “\t”.'’.$before.’‘.$text.’‘.$after.'’."\n”;
Hope it helps someone.
Great Hack - I have used a number of methods to get this type of functionality, mostly relying on querystrings.
Would be nice to be able to break it out of an list. The bullets don’t work well with the theme on my site.
Other than that - great.
J.
hi there,
i`m using the same_cat plugin but wondered if you can use this twice on one page?
i currently have included the same_cat plugin from the news directory to get the latest 5 news articles on the homepage. but on the left hand side of the page i want to be able to grab the latest 5 entries from our ‘missions’ wp install on the same page.
is this possible?
my site is here: www.startrekuk.com
grateful for any advice you can offer.
Hi, I have been trying to modify the code a bit to display posts’ excerpts next to their link in the list but I am having a bit of trouble. To get the excerpt, I’m trying to use $apost->post_excerpt in the foreach ($catposts as $apost) loop. For some reason the excerpts never turn up. Do you happen to know why this doesn’t work? Thanks.
Hi - I use this plugin (it’s great!) on my site but it has a bug – sometimes I post to the future – and this plugin shows posts even if the post date is in the future.
How can I fix it so this is not the case?
@ Neil - Yep, you can call it multiple times, with different categories.
@ Jonathon - You’ll have to debug it a bit more than that. If the excerpts are there, and you’re echoing properly, they should show up. Verify that post_excerpt has data in it.
@ Joe - I can try to add some code to prevent future posts from showing up.
I’ve debugged it as much as I know how. The excerpts exist and if I copy the code verbatim into the main loop on my index page, then the excerpt will show up fine. For some reason it won’t show up when it is used in the foreach ($catposts as $apost) loop in the same_cat.php file. The title and name work fine if I substitute post_name or post_title for post_excerpt in my code, but if I try to get excerpts or even the post id, it won’t show up… I may be doing something silly, but I simply can’t find the problem. I can send a copy of the modified same_cat.php if that would help. Thanks for the help.
This plugin is terrific! I’m trying to use it, however, to test for two categories not one before creating the link (I’m using it in the side bar on a CMS to show the elements of the current issue by matching both an “issue” category and, for example, an “articles” category, e.g. (brackets removed):
?php echo list_posts_of_cat(’1,8′, 1, 0); ?
…would show the link only if tagged to both cat1, cat8. Can this be done?
Keep getting the following: “Fatal error: Call to undefined function: array_fill() in /home/hikdekedek/public_html/barrassmonkey/wp-content/plugins/same_cat.php on line 125.” I’ve tried putting the suggested code inside the loop and inside, changing parameters, reinstalling plugin, but it just won’t work. (Sorry, URL above not actual site, which can’t reveal).
[…] first/all posts on index/other pages related posts - tried using SameCat, no joy, report here Excerpt size reduction on category/date archive pages Do you want comment moderation? Format static p […]
Great plugin….is there a way to put mutliple categories in here? Or, more specifically, is there a way to exclude a category (so that I can have posts from every category except one)? thanks…
can i define a category rather than list from the same category, and if so how?
This is a fantastic plugin, I was suprised a feature like this doesn’t come with WP.
AOLer time: is there a chance you could support sub-categories?
As in:
-Article 1
-Article 2
-Subcategory
–SArticle 1
Would love to this function up and running, but when I put it in place everything below it in my page no longer displays.
Here is my usage:
I have a problem with Wordpress v1.5 - Plugin name: SameCat. It functions, but with “$not_post” (use $post->ID to exclude the current post ID from the results) I don’t receive a result. A fixed ID e.g. “15″ functions. But “$post->id” not! Can you help me? Thanks.
Hello,
This is a really cool plugin you’ve developed, thank you. I installed it to my Precious Collectibles directory - more specifically, you can see an example in the specific categories and I have clean URIs turned on.
My problem however is with the feeds not working properly with the clearn URIs - I narrowed it down to your plugin. I continue to receive the error
Warning: Invalid argument supplied for foreach() in
/home/username/public_html/collectibles/wp-commentsrss2.php
on line
16
I have disabled comments and removed the reference to the comment includes for my particular layout and was wondering if there was a workaround to get it running smoothly with the clean URIs. Currently, if I hardcode the feed, i.e. wp-rss2.php, it works just fine, but it would make life easier to have WP automatically generate the feed URIs for me as I plan to set up category specific feeds (and there will be a lot of categories).
If it makes a difference, the only other plugins I currently have are Get-A-Post, Keywords, the_excerpt reloaded, and WP-Amazon. If there’s any known issues with any of these plugins I’d like to know.
Any help you can provide would be most appreciated. Thanks a million and one
I doubt that the “Invalid argument supplied for foreach()” bug is related to this plugin. I have not installed the plugin and I get the same behaviour.
From my 5 minutes of research, I believe that this is a known problem with some versions of WordPress.
According to the this posting (http://wordpress.org/support/topic/18314), the problem is fixed in the new version.
Cheers,
Stuart.
I can’t seem to get samecat to work.
In the sidebar if I specify what category I want (?php echo list_posts_of_cat(’x', 5, 5); ? where x is the category number) it’s no problem.
But I can’t get it to grab the category number from the entry it’s listed next to. When I use ?php echo list_posts_of_cat(get_the_category(), 5, 5); ? it says “no matching posts”
I feel like I’m missing something obvious.
I’ve had this plug-in running for some time. I usually use mozilla browser and no problems, but I checked my site in IE and the same cat display in the second and third post on the main page is wonky.
It’s all jammed up against the left hand side of the page. Any ideas on this?
Well, I’d start by validating and fixing your site – it came up with 60 (cascading) errors when I checked it. Possible any of those could be affecting things. Next, I might try ‘wrappering’ the SameCat output in a [div] block. Last, have you tried the CG-SameCat version in the CG-PowerPack? In any case, this looks like a general CSS/HTML issue.
Thanks David. I’ll look into this.
Hello David,
I want to use your samecat plugin and it’s working ok with WordPress 2.0.1. I never use multiple categories and would like to have the title of the current category above the list of posts. Single-cat-title() won’t work because the $cat is not present as a variable on the posts pages. I tried to just put single-cat-title() inside the samecat.php (silly), but with no success.
Could you tell me please how I could do this?
Thanks!
Jack
If you already know you are on a category page, or showing just one category, I believe you can print the category title with:
$singlecat = get_the_category();
echo $singlecat->cat_name;
I could potentially add a flag to print the category name, but the formatting then starts to get tricky as everone wants something different…