568

How to display the latest post from each category using wordpress query_posts

Wordpress Tips Tricks Tutorials

By default Wordpress presents all the posts in the blog sorted descending to post time at the home page. Well in some circumstances you might not want to have that layout on your home page. Lets say you have five categories and you want to pull the latest post from each of the categories and display it on your home page. Now that wont work out with the default Wordpress home page layout. Here is a simple tip to work around that situation. Wordpress has a function to filter out the posts depending on certain criteria, the function is known as . The template function query_post has several parameters that enables you to filter the posts for the Wordpress . Below is an example of pulling the latest post from a category and display it at the blog home page. The function can be modified by many parameters. Here I’m displaying only few parameters in action.

Example: Display the latest post from a category at homepage or display each latest post from multiple categories. This file is the index.php of your theme file.

<?php get_header(); ?>

<div id="content">

<!-- Latest from Category Sites -->
<?php query_posts(
            array('category_name'=>'Sites',
            'showposts'=>1,
            'order'=>DESC)
      ); ?>

  <?php if (have_posts()) : ?>

		<?php while (have_posts()) : the_post(); ?>

	<div class="post" id="post-<?php the_ID(); ?>">
		<div class="title">
			<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
			<span class="categories">Posted Under - <?php the_category(', '); ?></span>
      <p>Written by <?php the_author() ?> on <?php the_time('F j, Y'); ?>.</p>
		</div>
		<div class="entry">
    <?php the_content(''); ?>
			<span class="readmore"><a href="<?php the_permalink() ?>#more-<?php echo $post->ID; ?>">Read More</a></span> |<span class="addcomment"><?php comments_popup_link('Add Comments', '1 Comment', '% Comments'); ?></span>
		</div>
	</div>

		<?php endwhile; ?>

	<?php else : ?>

	<div class="post">
		<div class="title">
			<h2>No Archive Found</h2>
		</div>
		<div class="entry">
			<p>Sorry, but you are looking for an archive that isn't here.</p>
		</div>
	</div>

	<?php endif; ?>
<!-- End of Sites Category Display -->

<!-- Latest from Downloads -->
<?php query_posts(
            array('category_name'=>'Downloads',
            'showposts'=>1,
            'order'=>DESC)
      ); ?>

  <?php if (have_posts()) : ?>

		<?php while (have_posts()) : the_post(); ?>

	<div class="post" id="post-<?php the_ID(); ?>">
		<div class="title">
			<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
			<span class="categories">Posted Under - <?php the_category(', '); ?></span>
			<p>Written by <?php the_author() ?> on <?php the_time('F j, Y'); ?>.</p>
		</div>
		<div class="entry">
    <?php the_content(''); ?>
			<span class="readmore"><a href="<?php the_permalink() ?>#more-<?php echo $post->ID; ?>">Read More</a></span> |<span class="addcomment"><?php comments_popup_link('Add Comments', '1 Comment', '% Comments'); ?></span>
		</div>
	</div>

		<?php endwhile; ?>

	<?php else : ?>

	<div class="post">
		<div class="title">
			<h2>No Archive Found</h2>
		</div>
		<div class="entry">
			<p>Sorry, but you are looking for an archive that isn't here.</p>
		</div>
	</div>

	<?php endif; ?>
<!-- End of Downloads Category Display -->
<!-- Rest of the codes-->

 

Example: Exclude some categories from the loop

<?php
   if (is_home()) {
      query_posts("cat=-1,-2,-3");
   }
?>

 

Example: Retrieve a single specific post

<?php
  // retrieve one post with an ID of 5
  query_posts('p=5');
?>

 

Example: Get a sticky post

<?php
array('post__in'=>get_option('sticky_posts')); //returns array of all sticky posts

// To return just the first sticky post in a query
$sticky=get_option('sticky_posts') ; 

query_posts('p=' . $sticky[0]);
?>

 

These are just simple examples, you can do a lot more if you take a look at the Wordpress Codex for .

GD Star Rating
loading...
How to display the latest post from each category using wordpress query_posts7.5102

About Ruhani Rabin

Ruhani Rabin is the original owner and author of this blog. He also reviews web 2.0 startups at Tech2all.com. Largely interested in web 2.0 apps & Social Media. Currently the Vice President of MOL Access Portal (MOL is owner of Friendster.com). Also he is Web 2.0 & Social Media Researcher and a Total fun Geek!.. There you have it ;)

Twitter Profile: http://twitter.com/ruhanirabin

  • itjobs1
    thanks
  • awesome. thanks for your tutorial. it can help to filter post by category
  • meda
    Do you place these code in the loop or out of the loop?

    I tried using this but it said No Archive Found.
  • This code should be inside the loop
blog comments powered by Disqus