<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
If you were to use this, for example, in a left sidebar which ran before the main loop on your page, remember to reset the query or it will upset that main loop.
<?php wp_reset_query(); ?>
Is there a way to display a certain number of items and then have a link at the bottom for older posts?
Yes to question1. I have imported the most recent posts of a category into my website by using
query_posts('category_name=whatevernameyouuse &posts_per_page=6');
I have not looked at linking just to older posts, I just linked to my blog but I bet there is a way to do it…
Or for a more authoritative look at how to do recent posts, look on this website for the recent posts article. https://css-tricks.com/snippets/wordpress/recent-posts-function/
veru nice..
thanks for sharing..
i need this tuts for my web project..
I want to create a loop that contains only posts that are in two specified categories – I just don’t get how to do this. Is anyone here to help me out of this problem?
See where it says cat=5?
<?php query_posts('cat=5'); ?>
Make it say cat=5,6,7
Then, you will have fetched posts from categories 5, 6, and 7.
If you want to limit the number of posts, stick a “showposts” in there to make something like this:
<?php query_posts("showposts=2&cat=13,14"); ?>
This is awesome. Where are the valid places I can use this snippet of code? I want to display a custom loop on a page.
where I have to place
php wp_reset_query();
?I personally place it after the
?php endwhile; endif; ?>
I don’t know if it is a good practice but seen to work fine for me!Hi,
Your post was a great help.
The only difficulty I am having is setting the category to show from an id entered into a custom field when writing the post.
I’ve tried PHP echo and grabbing the field using a plugin but still struggling to set the category based on what the custom field value is.
Any help would be greatly appreciated.
Ben
Hey Chris,
How would you add an if empty statement to this? Many thanks
hamish
Thanks Mate! It really saved my life!!!!!!!
can i get the code to display the rest post of the same category in single page on which the viewer is in?
thanks a lot for sharing this with us thanks
Nice. Very well explained.
Thanks
Hi there, can anyone please tell me how to query custom posts form a certain category? I’ve tried everything. Thanks!
I’m also looking for the same answer as DSQ, when I’m running query_posts – this quires all posts in the db instead of the category you are on… Presuming because it’s outside the loop, or because there is no loop.
Is there any way you can query the loop? So for example I have a category page and I only want to display posts with the tag ‘example’ in that category, is this possible?
I’ve been at it for hours! Also just found this which has given me some new ideas http://wp.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/
Is there a way to do this so that it puts all the post from one category under that category and then loops to do the same for the next category. I Basically want it to do some think like this: Category 1 – Post 1 – Post 2 – Post 3 Category 2 – Post 1 – Post 2 – Post 3 Category 3 – Post 1 – Post 2 – Post 3 etc.
I used the almost the same code as advised on this post, minor tweaks
<?php query_posts('showposts=2&category_name=slug'); ?>
so, showposts sets how many posts are displayed
category_name=slug works great now that category ID has been deprecated.
only 2 posts wil show in the ofnt end
Totally used this today, but modified it a bit as I needed to include all posts except the ones that are in the home page slider category. To do this, just add a minus sign in front of the integer like so:
Thanks for the post!
Is there a way to have a post, rotate at midnight or daily from a specific category and not repeat for a year?
Hi there! I have a problem i hope someone can help me because i’m freaking out! I need to display the latest 4 post in my home page, but the problem is that i can’t use a simple loop because they are located in different part of my home page. So if I simply use a loop it will display me the latest 4 post in the space of 1. I kind of need to treat like an array so i can say” In this position show the last post” and ” in this other position show the one before the last” etc. I hope i explained myself pretty clear! Thanks to everyone.
This snippet was very usefull for me.
Thanks. It works fine.
How is it possible to show posts from different categories on different pages?
I want to display posts from category7 on page1, posts from category3 on page2 and so on.
I’m struggling with the php-code for the if-loop. Can anyone explain a little bit?
Thank you.
Here we can use wp_get_recent_posts() function directly for get posts
It looses the sticky functionality, how to keep it?
Here’s a question.. I have categories that I want to display on specific dates. Is there a way to run the loop to display posts of a certain category depending on the date? Like if I have a category named Jan-12, and I want to modify the loop to look at the current date and find/display the category with the same month/day (no year), is this possible?
I want to make my mobile press theme display posts for different categories at the index page how can i do this?
Great! Thanks a lot!
This is pretty bad practice now, query_posts should be skipped and get_posts used instead.
Thanx, great help!
my question is that i am not sure where to place the code to limit eg the categories (so to say pick posts from 3-4 categories).
Which php files from the theme am using shoud be altered?
I have on the right side bar a recent posts block and a slider on my first main page which displays posts.
thanks in advance!!
Nice ! You saved my website :’) <3<3
http://www.ieltsbucket.com
im unable to show some post on front page this code doesn’t work for me ? i’m showing featured category on front page
Thanks for posting this article.
I have a situation like i need to display 3 posts of category “sports” in a div .And In a another div i want to display the next three posts from the same “sports” category.Please give me suggestions regarding this.
thanks in advance.
Totally new to WP and came across this recently, partly because my pagination for posts in a category is not yet working..
https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts
You are using query_posts and afterwards resetting the query. By reading through above SE question and detailed explanation would you say it is still OK to use query_posts in this particular case?
Thanks all!
How can i ask the div container to display none if there are no posts in the specific category?
Thank you
Hello!
How can I fetch Specific posts from a category to which a user subscribed. Any ideas on the PHP Code? (Users subscribe to categories using https://wordpress.org/plugins/subscribe-to-category/)
Thanks so much!!
I want to loop single posts page by recent post right after the post and then again next recent posts with infinite loop. How can I do that?