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

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Learning about using multiple Loops is the best way.

    Thread Starter gillsans

    (@gillsans)

    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; ?>
    Thread Starter gillsans

    (@gillsans)

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

    Thread Starter gillsans

    (@gillsans)

    <?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; ?>
    Thread Starter gillsans

    (@gillsans)

    sorry about the indenting

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display post excerpt from specifc category’ is closed to new replies.