Top Post from Category Plugin
This is a plugin for WordPress 2.7. It was developed initially for GothamSchools.org and has been bundled up and released on WordPress.org.
It is basically a sidebar widget that shows the top post from a particular category plus its thumbnail. The excerpt from the post can be overridden.

Installation
- Download the plugin from wordpress.org.
- Unzip the plugin into the /wp-content/plugins directory on your webserver.
- Activate the plugin.
- In your sidebar, add the ‘Top Post from Category’ widget. You can also add several instances of this widget if you want.
- Open the widget options and specify what category ID to pull the top post from.
- You can leave the Excerpt blank for the default post excerpt or you can enter text to override that value.
Changelog
v. 0.3
- changes the category selector to a drop-down list in the admin
- is now a multi-widget—allows for multiple instance of the widget in a sidebar showing top posts from different categories
v. 0.2
- removes the dependency on the excerpt reloaded plugin
- fixes the “more” tag which used to disappear when you overrode the excerpt
v. 0.1
- initial release
Please add any questions or suggestions using the comments form below.
January 8th, 2009 at 1:28 pm
Great plugin!
January 10th, 2009 at 9:26 am
hi. Thanks for plugin
perfect.
Regards
January 10th, 2009 at 10:04 am
Hi, i like this plugin But i have a problem.
when i activate the widget on sidebar, it make an error because said that not there are the call of the fuction.
I have to add some in sidebar.php? Thx
January 10th, 2009 at 10:04 am
oh, sorry. I’m Italian so my english isn’t so good. I hope u can understand me.
January 10th, 2009 at 10:58 am
@LeFava: what is the exact error message? And what version of WP are you using?
January 10th, 2009 at 5:11 pm
I using 2.7 version. Maybe is only my theme that is a little old.
The error is this:
Fatal error: Call to undefined function the_excerpt_reloaded() in /home/user2427/public_html/wp-content/plugins/top-post-from-category-widget/top-post-from-category.php on line 60
I thin that i have only to add on sidebar.php the function that call plugin, but, and this is the really prblem for me php is like a sky without stars.
January 10th, 2009 at 5:53 pm
@LeFava: Ah, this is my fault. Right now this plugin uses the excerpt reloaded plugin: http://guff.szub.net/2005/02/26/the_excerpt-reloaded/
You’ll need to download that one too and activate it. Then the top post widget should work.
I should really upload a version of this plugin that doesn’t use the excerpt reloaded. I’ll put it on my list
January 11th, 2009 at 4:54 am
Perfect, now work. Really thx. I have another question. For add a thumbnail with the plugin, i have to insert that manually? or i need something?
January 11th, 2009 at 4:58 am
@LeFava: the plugin should search your post for an image. If one exists, it will grab the thumbnail version and display it.
January 18th, 2009 at 9:54 am
Great plugin! I wanted to know, does it show only the most recent post published? That seems to be the case for me. Or does it show the top post for any category I choose?
January 18th, 2009 at 10:00 am
@Ed It should show the most recent post from the category you choose. Is this not how it’s working for you?
January 18th, 2009 at 2:55 pm
Well, say I change the category to another. It still displays the most recent post. Should I delete and re-install?
January 18th, 2009 at 3:54 pm
@Ed: If you change the category to another, it should grab the most recent post from that category. Make sure you click “Save Changes” etc.
One thing that might be causing a problem, is that the if you explicitly set the excerpt text for the widget, it doesn’t automatically get erased when the category is changed or a new post is posted. So make sure that you erase it. Does this help?
January 18th, 2009 at 7:54 pm
I found a solution. Instead of the category name, I put in the numerical category ID.
I’m glad its workin now! Great Plug-In!
January 19th, 2009 at 10:06 am
@Ed: Cool. I’ve made a note to install a category drop-down picker when I get around to it.
January 29th, 2009 at 7:16 am
Hi! How can i make this show the three latest posts instead of only one?
February 2nd, 2009 at 10:25 am
@iKeg: if you edit this file:
top-post-from-category/top-post-from-category.php
query for 3 posts instead of 1 and put a loop around the post output that would show you the top three I think.
February 6th, 2009 at 3:10 pm
hello,
i’m looking for this kind of plugin for over 6 months.
but i got a problem. can you find a sollution to let widget on sidebar for a more instances ?
thanks , man !
February 6th, 2009 at 3:45 pm
@gourme: I think what you mean is how do you have multiple instances of this sidebar widget. Is that right?
To do this you’ll need to edit top-post-from-category/top-post-from-category.php around line 138, try adding the lines:
register_sidebar_widget(array(”Top Post from Category 2″, ‘widgets’), ‘widget_toppostfromcategory’);
register_widget_control(array(”Top Post from Category 2″, ‘widgets’), ‘widget_toppostfromcategory_control’);
And now you should have two copies of this widget.
February 10th, 2009 at 12:55 pm
Thanks for your answer.
But there’s a problem. When i try to change the id category of first instance widget, automaticaly is change the id categ. at the second instance. What shoud i do ?
February 12th, 2009 at 8:17 am
Hi Thanx for plugin! wanna ask you - can i use it not only in side bar? for example in main page? i mean use som code?
February 12th, 2009 at 11:06 am
@gourme: I see. Well, I think you could find some sort of hack solution by making two copies of top-post-from-category. Then in top-post-from-category1.php, you’ll have to change all references to toppostfromcategory. So on line 31, instead of this:
$options = get_option(’toppostfromcategory’);
change it to this:
$options = get_option(’toppostfromcategory1′);
Same with lines 91, 95, etc.
If you just do a find/replace command through the file it should then create a duplicate widget with “1″ appended to all instances of toppostfromcategory. This should then work independently of the other widget instance.
February 12th, 2009 at 11:09 am
@Alex: Sure you could grab some code to show a top post in your main template. If you grab these lines:
$my_query = new WP_Query(”cat=$category&showposts=1″);
$my_query->the_post();
You’ll be able to show the top post from $category in your main page with:
< ?php the_title(); ?>
etc.
February 13th, 2009 at 11:02 am
hello. i’ve changed all instances of “toppostfromcategory” with “toppostfromcategory1″ . When i try to activate the seccond php file, i get this: ” Cannot redeclare get_the_thumb()
(previously declared in /home/…/public_html/wp-content/plugins/top-post-from-category1.php:73) in /home/…/public_html/wp-content/plugins/top-post-from-category.php on line 86 ”
any solutinon?
February 13th, 2009 at 11:28 am
@gourme: delete the whole get_the_thumb() function in the second file
February 14th, 2009 at 8:48 am
Hello, I can’t see any output from this plugin, either on the page or source. It installed ok and I’ve selected a category in the widget, but nothing shows, not even the widgtet title. WP-2.71 default theme
Any advice please?
February 14th, 2009 at 9:16 am
After some more testing it seems to be a theme related problem. It would be easier if I could hard code the widget into my sidebar ibstad of using a widget. What code can I use for this please?
Thanks…
February 14th, 2009 at 3:56 pm
@Richard, I’m not sure what the problem is and why it doesn’t work in your theme. Maybe your theme does not have a widgetized sidebar. You can widgetize your sidebar by following these instructions:
http://bit.ly/19y0XI
Alternatively, you can just strip out the code you need and hardcode it into your sidebar. You’ll need the code in the widget_toppostfromcategory() function from top-post-from-category.php. You’ll need to put in values for the options.
February 14th, 2009 at 4:24 pm
Thanks for the reply CJ. My theme is a 2.7 widgetised theme and other widgets run
ok. It’s not a plugin issues as I now have it working when |I use the default theme. I’ll try the hard coding option as you suggest and hope I can get it to work . Thanks.
February 14th, 2009 at 5:10 pm
@Richard: Another thing you might want to try is to play around with the order of the widgets in your sidebar. Maybe one of the other widgets is conflicting with it. If you only have this Top Post widget in your sidebar, does it work then?
February 15th, 2009 at 3:10 am
Thanks for the tip CJ. I’ll play around with this today and let you know how I fare
February 23rd, 2009 at 1:15 pm
Hi, СJ. thanks for an answer. Have another one question. i`m not a guru in php:)) so, wanna ask you. how i can make to show top post from each category, i mean for example, on the mainpage i have to show 3 top post from 3 category:
Top Post from cat 1 Top Post from cat 2 Top Post from cat 3.
How i can do it? thanks a lot.
February 23rd, 2009 at 1:45 pm
Hi @Alex, I gave @gourme some advice on how to do this type of thing (see above). It’s kind of messy but I think you’ll need to make three copies of the plugin and change some of the var names in each. That would be the quickest way.
February 23rd, 2009 at 2:02 pm
CJ@But the category have some ID, isnt easy to write some code to get the id of cateogory and show the post?
February 23rd, 2009 at 2:31 pm
@Alex: I’m not sure I understand what you mean.
March 11th, 2009 at 9:43 am
Hi, thanks for great plugin! I wonder if I can customize it so that it shows, say, 3 most recent posts in some category? And another question, how does it decide the size of the thumbnail, could I modify it somehow?
March 11th, 2009 at 10:39 am
@Lariza, see http://cjyabraham.com/projects/top-post-from-category-plugin/#comment-29 above for an answer to your first question.
You could probably modify the size of the thumbnail in css working with the class “thumbnail”. Otherwise you could edit the markup in top-post-from-category.php:70.
BTW, I just released a new and improved version of this plugin
March 11th, 2009 at 12:25 pm
thanks! sorry to bother you, but my php skills are really lousy - how do I “put a loop around the post output”? I have now put it like this in top-post-from-category.php:
<a href=”">
etc.
But it’s now showing ALL the posts in every category. I have modified line 46 like this: $my_query = new WP_Query(”cat=$category&showposts=3″);
What did I do wrong?
thanks in advance!
March 11th, 2009 at 12:28 pm
oops, the code vanished. Anyway, I put the beginning of the loop just before div class=post and the ending after
March 11th, 2009 at 12:29 pm
…ahem, after the div closing tag.
March 11th, 2009 at 1:12 pm
@Lariza: I think you’re on the right track. You’ll probably need to put the loop before this line:
$my_query->the_post();
Post your code to pastebin.com and I’ll take a look.
March 11th, 2009 at 1:58 pm
Ok - I did what you suggested and now it’s showing 3 last posts but strangely, after them three times the thumbnail of the latest post (without texts, just the thumbnail and More..-link). I posted the code to pastebin.com. Thanks alot, you’re really helpful!
March 11th, 2009 at 2:11 pm
@Lariza: I got it working for myself. Copy the following code:
http://pastebin.com/f4f41a983
March 11th, 2009 at 2:12 pm
… in here: http://pastebin.com/d5781e47a
March 11th, 2009 at 2:14 pm
THANKS!!! Works perfectly now!
March 12th, 2009 at 5:51 am
Hi again - I might just celebrated to early!
The problem is now that the widget is somehow conflicting with the other sidebar items, which are not widgets in the dynamic sidebar but hard coded in the home.php. If I put the code calling for the dynamic sidebar right in the beginning of the sidebar, it works fine, but as soon as I put it somewhere else, TPFC disappears. Other widgets (for example basic text widget) don’t behave like this, they show up wherever I have put the dynamic sidebar. Do you have some idea what’s causing this? Sorry to bother you again…
March 12th, 2009 at 8:26 am
@Lariza: I’m not sure what the problem is here. I’d need to take a look at your code. Post it to pastbin if you want.
March 12th, 2009 at 10:19 am
Hi Chris, I put the code of my home.php here: http://pastebin.com/d4f020de4. Thanks!
March 13th, 2009 at 11:27 am
ok - I solved it with my non-existent php-skills!
The problem was somehow caused by the if (is_home()) -condition. I removed it because I’m anyway using this sidebar only in my home page so the plugin doesn’t need to detect where it is… I don’t know exactly why it was like this, but now it’s working for me!
March 13th, 2009 at 6:30 pm
@Lariza: Hmm, ok. Glad that you solved the problem. I’ll keep an eye out for other complications of this is_home() clause next time I’m in the code.
March 24th, 2009 at 8:38 am
I wonder why I only get the plugin to work at my homepage - it won’t show up in the sidebar of a page. ?
March 24th, 2009 at 3:27 pm
ok - I removed the if (is_home())
March 28th, 2009 at 12:10 pm
Great plugin but,
The text it displaces runs off the screen as if it it’s not recognizing the width of the sidebar.
Solution?
check out my site.
March 29th, 2009 at 5:36 am
Great plugin, just what I was looking for! Does any1 know where can I edit the length of the preview text and is it possible not to display one, but two or more latest posts from certain category!
BTW, the Gert for your post, it helped me alot!
March 30th, 2009 at 4:26 pm
@ricland: is this still an issue for you? I couldn’t find anything about it on your site. Try using skitch.com for a screenshot.
March 31st, 2009 at 9:53 am
@Darco: you’d need to edit top-post-from-category.php around line 77 to limit the size of the excerpt that is generated. And for displaying two or more posts, see the comment thread above ending with:
http://cjyabraham.com/projects/top-post-from-category-plugin/#comment-74
June 20th, 2009 at 3:56 pm
Hi,
I hope you can help. When I add the top post widget to my sidebar and select edit, the widget is deleted from sidebar instead of opening the pop-up box to select the settings.
The widget remains listed as an “available widget” but does not allow me to “edit.” I’ve tried several times, refreshed the page and logged in and back out again.
Thank you.
Best regards,
Gina
June 22nd, 2009 at 9:57 am
@Gina: are you using WP2.8? I’m not sure it works in 2.8.
June 22nd, 2009 at 10:05 am
Hi Chris,
No, I’m using 2.7.1.
Thanks,
Gina
June 22nd, 2009 at 10:08 am
@Gina: Hmm. That’s weird. I’m not sure what the problem is. Maybe it is conflicting with another widget in your sidebar. As an experiment you could try removing all other widgets and seeing if you still have a problem.
June 22nd, 2009 at 10:21 am
I tried but it didn’t fix the problem. Do I need to add code to the sidebar template?
June 22nd, 2009 at 10:22 am
@Gina: Well you need to have a widgetized sidebar. Do other widgets work in your sidebar? Maybe this widget doesn’t work with wp2.7.1. I’ve only ever tested it up to wp 2.7.
June 22nd, 2009 at 10:27 am
Yes, I have a widgetized sidebar and the other widgets work. Not sure what the problem is but I thank you for trying to help.
July 12th, 2009 at 3:02 pm
I really love this plugin
August 16th, 2009 at 11:49 pm
Hi Chris,
is there a chance or the possibility that the widget shows 5 or more random posts of the whole blog instead the top one of a category and in my wished case only as thumbnails without any text or something?
Thx in advance and greetings from germany
Dennis from the Babbel-Net Team
August 17th, 2009 at 2:21 pm
@Dennis: Showing five random posts isn’t how this widget works but it would be easy enough to design a widget that does that.
August 17th, 2009 at 3:43 pm
Hi Chris,
would you be please so kind and design something like this? Because your widget is the only plugin I found that is very close to the idea I have in mind and I have no clue how to code such widgets.
Thx in advance
Dennis from the Babbel-Net Team
August 18th, 2009 at 11:47 am
@Dennis: I don’t have time at the moment for any freelance work but another WP developer should be able to do this without much trouble.