Sticky Posts per Category

WordPress-Logo

 

Using WordPress as a CMS,  I needed the sticky posts to display on the top of the page per category and then display the rest of the post (in the category) in alphabetical order by title.

I ended up running 2 loops, 1 for sticky post and 1 without. I created a category.php file and I’m using the follow queries:

 

// get the current category
$category = get_the_category();

// LOOP 1 get the sticky post in the category, order by title - ascending
query_posts(array( 'post__in' => get_option('sticky_posts'), 'orderby' => 'title', 'order' => 'ASC' , 'cat' => ''.$category[0]->cat_ID.'' ));
Do your loop for posts.....

// We all ready have the $category array
// LOOP 2 get all the posts (non-sticky) in this category order by title - ascending
query_posts(array( 'post__not_in' => get_option('sticky_posts'), 'orderby' => 'title', 'order' => 'ASC' , 'cat' => ''.$category[0]->cat_ID.'' ) );
Do your loop for posts.....

This is the solution that worked for me.  I’m guessing there might be a easier way to do this, but I couldn’t find one.

About: Jason Huber

Jason Huber is a multi-disciplinary, award winning web developer specializing application development and user experience.

20 comments

  1. Blogging Park says:

    Hi jason I know this is not a related comment here but i need to contact you.Its very urgent.I am, using simplycore theme for my wordpress blog.I want to add multitab widget in my theme.There is a wordpress plugin to install but its not supporting simplycore theme,according to the plugin setup I have to edit my function.php file.But I don,t want to do it as I have bad experience about editing function php file.So let me know how it can be possible,there was also suggestion to contact theme creator,so I am here to tell me the solution.Please provide me some guide.Thanks.
    Manas Kabiraj

  2. Jason Huber says:

    Hi Manas,

    This plugin http://wordpress.org/extend/plugins/tabber-tabs-widget/ will work with the simply works core.

    Thanks,
    Jason

  3. Blogging Park says:

    thanks jason for the link, I will try it soon.

  4. Lavvy says:

    Hey Jason,

    I’m trying to reach out to you regarding your theme as well. I’m having trouble removing the ‘add comment’ link from posts when I’ve already disabled the ability for someone to comment. I’m not sure where to change that in the theme code. Can you help?

    thanks
    Lavvy

  5. Jason Huber says:

    Hi Lavvy,

    When creating (or editing) your post / page uncheck “Allow Comments” in the Discussion box. If you do not see the Discussion Box (while creating post or page) click on “Screen Options” in upper right corner and check the Discussion box. Then it will appear.

    You could change the code in the theme, but I when I get version 1.4.9 completed your change will be lost during the upgrade.

    thanks,
    Jason

  6. Peter Cook says:

    Dear Jason
    First of all I want to thank you for the very schmick Simply Works framework. I confess I am a newbie and I have just installed it and was pleasantly surprised to find that it was actually really easy to use. However, I have one question: I would like to put a light grey background colour behind the posting area, ie, the part where you type the text for the post (similar colour as you have already appearing in the generic footer). I find that it avoids some of the screen glare if there is a soft colour behind the text rather than plain white, when reading the text. However, I can’t figure it out, is there an easy way to do that? Or if not an easy way, maybe you can share the ‘harder’ way?

    Once again, thank you for providing an impressive framework.
    Peter
    (Brisbane, Australia)

  7. Jason Huber says:

    Peter,
    you will need to edit the style sheet. go to Appearance > Editor and look for the line below (around line 360 or so)

    change this:

    /************************************************
    * ^6 Mainbody Area
    ************************************************/
    #mainbody .wrapper {
    background: #FFFFFF;
    }

    TO THIS:

    /************************************************
    * ^6 Mainbody Area
    ************************************************/
    #mainbody {
    background: #E1DFDF;
    }

    #mainbody .wrapper {
    background: #E1DFDF;
    }

    thanks,
    Jason

  8. Elena says:

    Sorry, I would like to re-post the comment, a part of the code is missing :)

    Hi Jason,
    first of all I found this post very useful I am just starting with php though, and I have some doubt I hope you can help me with.
    I need to display on a page only the sticky posts from a certain category, but with my current code I got all my sticky posts displayed!

    I know how to display all posts from the category (but not in the desired order)
    /************************************************

    have_posts()) : $my_staff->the_post(); ?>
    /************************************************

    and this is how I call all the sticky posts
    /************************************************
    get_option(‘sticky_posts’), ‘order’ => ‘ASC’ , ‘cat’ => ”.$category[0]->cat_ID.” ));?>
    /************************************************

    How can I combine the two queries? Can you help?
    Thanks…
    Elena

  9. Kost Jakarta says:

    Hello Jason,

    I have 5 sticky posts and want to move their position regularly, is there any way to do this? Thanks.

  10. Whatafy says:

    Hi, ok i understand to copy the code to category.php, but where is that located ? i try last hours to find a solution, i use a theme junkie theme called weekly, how can i edit to have sticky post on categories ?

  11. Vero says:

    Hi Jason!

    The code works perfect, but pagination is broken after that… I’ve tried many $paged codes without success… any help please??

    Thanks!

  12. Jenny @ Savour the Senses says:

    Hi Jason,

    I have been using your simply works core for my blog, but was wondering if there was any way that I could have both the left and right sidebars at the same time? My option only allows for one or the other. Thanks!

  13. Jason Huber says:

    Sorry, the simply works theme only allows for one sidebar.
    Jason

  14. JM says:

    Thanks! Life Saver!

  15. JM says:

    By the way. I didn’t use it as is because it wasn’t exactly what I was looking for. I did a little change to it. So (for those who need it) if you want to display the sticky posts from certain category, you should use this
    get_option('sticky_posts'), 'category_name' => 'category-slug-here')); ?>.
    Thnaks again!

  16. Brian says:

    Created a plugin for this, theme agnostic. It stores stickies per category in a plugin option and then applies a filter to “the_posts” to modify the display order when viewing a category archive page with category stickies.

    Posts are assigned “sticky” categories using a meta box in the post editor.

  17. Jason Huber says:

    Brian,
    Has this plugin been approved through WordPress.org yet?

    Thanks,
    Jason

  18. Brian says:

    Hey Jason, yes it has. If you search the Add Plugins page in WP Admin for category sticky I believe its the first one that comes up.

    Brian

  19. Steve says:

    Hi Jason,

    Apologies for posting here but I can’t seem to find any other way of contacting you. I have just downloaded your theme and installed it – thank you. First one I’ve found that is user friendly for us newbies!

    I have a question though…I noticed that on your demo page, your blog posts are shortend and have a ‘read more’ button. Can you tell me how I can do that please? I have a few long posts that would really benefit.

    Many thanks for your help and the framework.

    Steve

  20. Jason Huber says:

    Hi Steve,

    In your post use the “Insert More Tag” in the top row of editing tools. This will create the more button for you.

    Thanks,
    Jason

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Copyright © Jason Huber .net
This site is mobile ready. How about you?