• I’d like to display the 3 most recent posts from multiple categories in two columns (in a 3-column layout). I cannot find the answer to this in Codex or support forums. Can someone tell me what I did wrong here…I am not a programmer and I just combined two different solutions but clearly is not working. Thank you for any assistance you can give!!

    <?php get_header(); ?>
    
    <!-- BEGIN content -->
    <div id="content">
    
    <?php $tmp_query = $wp_query; ?>
    
    	<!-- BEGIN left category posts -->
    	<div class="category">
    
    		$post_ids = array(0);
    foreach( array(10, 8, 7, 9) as $cat_id ) {
     if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 3)) ) {
     $first = array_shift($posts);
     $post_ids[] = $first->ID;
     }
    }
    query_posts(array('post__in' => $post_ids));
    		if (have_posts()) : $count = 0;
    		?>
    		<div class="box">
    		<h2><a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?></a></h2>
    		<ul>
    		<?php
    		while (have_posts()) : the_post(); $count++;
    		$titlewidth = 22;
    		?>
    		<li<?php if ($count==1) echo ' class="first"'; ?>>
    			<a href="<?php the_permalink(); ?>"><?php echo fs_clean($post->post_title, $titlewidth); if (strlen($post->post_title)>$titlewidth) echo '...'; ?></a>
    			<a class="attachment" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
    			<p><?php echo fs_clean($post->post_excerpt, 140); ?>...</p>		</li>
    		<?php endwhile; ?>
    		</ul>
    		</div>
    		<?php endif; endforeach; ?>
    	</div>
    	<!-- END left category posts -->
    
    	<!-- BEGIN right category posts -->
    	<div class="category">
    
    		$post_ids = array(0);
    foreach( array(4, 5, 6) as $cat_id ) {
     if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 3)) ) {
     $first = array_shift($posts);
     $post_ids[] = $first->ID;
     }
    }
    query_posts(array('post__in' => $post_ids));
    		if (have_posts()) : $count = 0;
    		?>
    		<div class="box">
    		<h2><a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?></a></h2>
    		<ul>
    		<?php
    		while (have_posts()) : the_post(); $count++;
    		$titlewidth = 22;
    		?>
    		<li<?php if ($count==1) echo ' class="first"'; ?>>
    			<a href="<?php the_permalink(); ?>"><?php echo fs_clean($post->post_title, $titlewidth); if (strlen($post->post_title)>$titlewidth) echo '...'; ?></a>
    			<a class="attachment" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
    			<p><?php echo fs_clean($post->post_excerpt, 140); ?>...</p>		</li>
    		<?php endwhile; ?>
    		</ul>
    		</div>
    		<?php endif; endforeach; ?>
    	</div>
    	<!-- END right category posts -->
    
    </div>
    <!-- END content -->
    
    <?php get_sidebar(); get_footer(); ?>
Viewing 1 replies (of 1 total)
  • Thread Starter wildbug

    (@wildbug)

    Well I figured out how to do this but can’t figure out how to make it display 3 posts from each category instead of 1. I used an alternate approach in the code:

    <div class="categorymain">
    
    <?php
    		$categories = get_categories('hide_empty=1&include=' . fs_settings('expertright_categories'));
    		foreach ($categories as $category) :
    		query_posts('showposts=' . fs_settings('expertright_category_length') . '&cat='.$category->cat_ID);
    		if (have_posts()) : $count = 0;
    		?>
    		<div class="boxmain">
    		<h2><a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name ?></a></h2>
                    <ul>
    		<?php
    		while (have_posts()) : the_post(); $count++;
    		$titlewidth = 45;
    		?>
    		<li<?php if ($count==1) echo ' class="first"'; ?>>
    			<a href="<?php the_permalink(); ?>"><?php echo fs_clean($post->post_title, $titlewidth); if (strlen($post->post_title)>$titlewidth) echo '...'; ?></a>
    			<a class="attachment" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
    			<p><?php echo fs_clean($post->post_content, 135); ?>...</p>		</li>
    		<?php endwhile; ?>
    		</ul>
    		</div>
    		<?php endif; endforeach; ?>
    	</div>

    Can someone tell me what to do to get the 3 most recent posts from each category display? ALSO, I don’t know how to create a space between the 2 columns. Currently, the title headers are melding into each other. How to create a small space between the two? Here is the page

Viewing 1 replies (of 1 total)
  • The topic ‘display recent posts from multiple categories on one page’ is closed to new replies.