display recent posts from multiple categories on one page
-
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)
Viewing 1 replies (of 1 total)
- The topic ‘display recent posts from multiple categories on one page’ is closed to new replies.