Support » Themes and Templates » list posts in certain category on homepage

  • Hello,

    I am looking for some code to list the latest 5 posts in a certain category. Can anyone help?


Viewing 8 replies - 1 through 8 (of 8 total)
  • This is what I use:

    <?php query_posts('cat=1&showposts=5'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        <?php the_excerpt() ?>
    <?php endwhile; endif; ?>

    Just change cat=1 to your desired category.

    Thanks for this Kalessin. It worked great.

    If i wanted to put this on a normal page. How would i do this? because it seems to just show the above code and ignore everything else.

    I have the following:-

    Template Name: Sign Up
    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    <div id="content" class="contact">
    	<div id="randreviews">
    		<?php query_posts('cat=6&showposts=5'); ?>
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    						$images = get_children(
    							'post_parent' => $post->ID,
    							'post_status' => 'inherit',
    							'post_type' => 'attachment',
    							'post_mime_type' => 'image',
    							'order' => 'ASC',
    							'orderby' => 'menu_order'
    						if ( $images ) {
    							$count = 1;
    							foreach ( $images as $id => $image ) {
    								if( $count === 1 ) {
    									$img = wp_get_attachment_url( $image->ID );
    									$link = get_permalink( $post->ID );
    									print "\n\n" . '<a href="' . $link . '"><img src="' . $img . '" alt="" /></a>';
    			<?php endwhile; ?>
    		<?php endif; ?>
    	<?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    		<h2><?php the_title(); ?></h2>					
    		<?php the_content(); ?>
    	<?php endwhile; ?>
    	<?php else : ?>
    		<h2>Not found!</h2>
    		<p><?php _e('Sorry, this page does not exist.'); ?></p>
    	<?php endif; ?>
    	<?php if(is_page(array('80'))) {
    	} ?>	
    <?php get_footer(); ?>

    Basically, you get your posts with query_posts() then you have

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    formatting for each post goes here
    <?php endwhile; endif; ?>

    That’s the end of the Loop. If you want to do another Loop, you have to start with a new query_posts() before you go into if (have_posts()) again.

    I’ve got a featured post category and used Kalessin’s code on my homepage to create links to featured posts, but somehow I end up displaying the posts in full right after that. I think I need to reset:

    <?php query_posts('cat=8&showposts=5'); ?>

    so it stops looking at cat=8 and looks for the most recent posts. Anybody know how I manage that reset or whatnot?

    I figured as much, I just didn’t know what to query. I did some experimenting and this worked:<?php query_posts('cat=0&showposts=1'); ?>. For future reference, insert the following code between
    <?php if (have_posts()) : ?> and <?php while (have_posts()) : the_post(); ?>

    <?php query_posts('cat=8&showposts=5'); ?>
      <h2>Featured Posts</h2>
      <?php while (have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
          <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php query_posts('cat=0&showposts=1'); ?>

    Obviously you’ll want to set the first cat= to the ID# for your featured content category and alter the showposts= to display the desired number of posts in the featured posts section as well as the normal number of posts on the main index.

    How can I do this with no post-display limit?

    In place of showposts=1 use posts_per_page=-1.

    See query_posts Page parameters

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘list posts in certain category on homepage’ is closed to new replies.