WordPress.org

Ready to get started?Download WordPress

Forums

list posts in certain category on homepage (9 posts)

  1. Dave Redfern
    Member
    Posted 5 years ago #

    Hello,

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

    Thanks
    Dave.

  2. Kalessin
    Member
    Posted 5 years ago #

    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() ?>
      </li>
    <?php endwhile; endif; ?>

    Just change cat=1 to your desired category.

  3. Dave Redfern
    Member
    Posted 5 years ago #

    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:-

    <?php
    /*
    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(); ?>
    					<?php
    						$images = get_children(
    						array(
    							'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>';
    								}
    								$count++;
    							}
    						}
    					?>
    			<?php endwhile; ?>
    		<?php endif; ?>
    	</div>
    
    	<?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'))) {
    		insert_cform('2');
    	} ?>	
    
    </div>
    
    <?php get_footer(); ?>
  4. Kalessin
    Member
    Posted 5 years ago #

    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.

  5. thebadone411
    Member
    Posted 5 years ago #

    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?

  6. t31os
    Member
    Posted 5 years ago #

  7. thebadone411
    Member
    Posted 5 years ago #

    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>
        </div>
    <?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.

  8. xdementia
    Member
    Posted 4 years ago #

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

  9. MichaelH
    Member
    Posted 4 years ago #

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

    See query_posts Page parameters

Topic Closed

This topic has been closed to new replies.

About this Topic