WordPress.org

Ready to get started?Download WordPress

Forums

Display post excerpt from specifc category (6 posts)

  1. gillsans
    Member
    Posted 6 years ago #

    Hi All,

    I have a custom template for my homepage and I want to display 2 separate categories from my posts. The categories I created are "Blog" and "News".

    I want to display a summary (or excerpt?) of the 3 most recent posts for each specific category.

    Whats the best way to do this? Here is a link to a screenshot:

    http://architexture.ca/NT/screenshot.png

  2. moshu
    Member
    Posted 6 years ago #

    Learning about using multiple Loops is the best way.

  3. gillsans
    Member
    Posted 6 years ago #

    Thanks Moshu, I managed to put a solution together that worked:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- If the post is in the category we want to exclude, we simply pass to the next post. -->
    <?php if (in_category('6')) continue; ?>
    <div class="post">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <small><?php the_time('F jS, Y'); ?></small>
        <?php
    	$strings = preg_split('/(\.|!|\?)\s/', strip_tags($post->post_content), 2, PREG_SPLIT_DELIM_CAPTURE);
    	echo apply_filters('the_content', $strings[0] .  $strings[1]);
        ?>
    </div><!-- closes the first div box -->
    <?php endwhile; else: ?>
        <p>Sorry, no posts matched your criteria.</p>
     <?php endif; ?>
  4. gillsans
    Member
    Posted 6 years ago #

    Now I just need to find out how to limit the output to 3 posts.

  5. gillsans
    Member
    Posted 6 years ago #

    <?php $my_query = new WP_Query('category_name=Blog&showposts=3');
      		while ($my_query->have_posts()) : $my_query->the_post();
      		$do_not_duplicate = $post->ID;?>
        <div class="post">
      		<?php //the_permalink(); ?><?php //the_title(); ?>
     		<a href="<?php the_permalink(); ?>"><?php the_time('F jS, Y'); ?></a>
      		<?php
    			$strings = preg_split('/(\.|!|\?)\s/', strip_tags($post->post_content), 2, PREG_SPLIT_DELIM_CAPTURE);
    			echo apply_filters('the_excerpt', $strings[0] .  $strings[1]);
    		?>
    	</div><!-- closes the first div box -->
      <?php endwhile; ?>
        <!-- Do other stuff... -->
      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
       <!-- Do stuff... -->
      <?php endwhile; endif; ?>
  6. gillsans
    Member
    Posted 6 years ago #

    sorry about the indenting

Topic Closed

This topic has been closed to new replies.

About this Topic