• Hi,

    I’ve looked all over and I’m just not able to find what I’d figured might be a relatively simple bit of code for producing a homepage that splits newest posts from specific categories into “boxes” on the blog homepage.

    Does anyone know how I might be able to do something like this screenshot?

    https://www.welcomebrand.co.uk/downloads/multiple.png

    Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • <div id="reviews">
    	<?php
             $blog_query = 'showposts=5&cat=210&paged='.$paged;
    	$posts = query_posts($blog_query);
    	while (have_posts()) : the_post(); ?>
    
    		How you want your post to look here:	
    
    	<?php endwhile; ?>
        </div>

    You can do a query at anytime inside your theme. Notice the cat= in the $blog_query line, that’s telling the query to display only that category.

    The showposts = is telling the query how many of those posts in that category you want to display.

    You can repeat that loop as many times as you want on your page within reason.

    Hi,
    is there any way that i can get post from multiple category using a single WP_Query.

    thanks in advance .

    I don’t understand this.
    I can’t drop this anywhere on the index page and have it work.
    I haven’t been able to get it to work with more than one category.
    I don’t understand the meaning of “how you want your post to look here”.
    Can anyone offer further explanations?

    @jamble:

    Create a file called home.php with something like this:

    <?php get_header(); ?>
    
    <div id="topbox">
     <?php
     $topbox = get_posts('numberposts=5&category=1');
     foreach($topbox as $post) :
        setup_postdata($post);
     ?>
     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
     <?php endforeach; ?>
    </div>
    
    <div id="leftbox">
     <?php
     $leftbox = get_posts('numberposts=5&category=3');
     foreach($leftbox as $post) :
        setup_postdata($post);
     ?>
     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
     <?php endforeach; ?>
    </div>
    
    <div id="rightbox">
     <?php
     $rightbox = get_posts('numberposts=5&category=5');
     foreach($rightbox as $post) :
        setup_postdata($post);
     ?>
     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
     <?php endforeach; ?>
    </div>
    
    <?php get_footer(); ?>

    Style it with CSS as you see fit.

    I’m trying to get this snippet working by pasting it into index.php.
    If I paste the entire snippet at the top of the file, the resulting page containers and font are misaligned.
    Since ‘How you want your post to look here:’ seems like it might refer to the CSS page styling, maybe the ‘<?php endwhile; ?>’ is meant to be somewhere further down the page, after some styling code.
    If I use the default page style (Kubrick), and paste the first 5 lines at lines 3-7 as follows,
    1 <?php get_header(); ?>
    2 <div id=”content” class=”narrowcolumn”>
    3<div id=”cat22-technology”>
    4 <?php
    5 $blog_query = ‘showposts=1&cat=22&paged=’.$paged;
    6 $posts = query_posts($blog_query);
    7 while (have_posts()) : the_post(); ?>
    and the ‘<?php endwhile; ?>’ after what might be styling, I get syntax errors.
    On top of that, if I add a second copy of the snippet with a different category, the result only shows the most recent category rather than each.

    Maybe there’s an answer out there waiting for me to find it.
    Something to do with the Loop, I know. But a bit more explanation here would certainly help everything fall into place.

    So, for example, this snippet based on what Frumph wrote goes in the index.php template, and lists the latest post in category 1:

    <div id="content">
    <?php $blog_query = 'showposts=1&cat=1&paged='.$paged;
    $posts = query_posts($blog_query);
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    But since I want to have the latest posts in categories 1, 2, and 3, I need to do something other than
    <?php $blog_query = 'showposts=1&cat=1,2,3&paged='.$paged;

    Probably something more like the repetition Stiand set out.

    Hey, this has been really useful. I have a slightly different project in mind. I’m using the Mimbo theme which highlights the most recent post in a lead div. I’m trying to modify it so that the lead post excludes certain categories, which I have pulled off, but how would I have it show a post if it is sticky?

    This is what I have:

    <div id="lead" class="clearfloat">
    
    <!---Loop One to Get Lead Story Excluding Category 5 (About KZBlog)-->
    	<?php
             $blog_query = 'showposts=1&cat=-5&paged='.$paged;
    	$posts = query_posts($blog_query);
    	while (have_posts()) : the_post(); ?>
    
    <!--Loop One Publish Details of Story-->
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
    <br />
    <!--Lead Post Image-->
    <?php $cti = catch_that_image(); if(isset($cti)){ ?>
    <img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $cti; ?>&h=230&w=230&zc=1" alt="Link to <?php the_title(); ?>" 
    
    class="thumbnail"/>
     <?php } else {?><img src="https://www.kzblog.net/wp-content/uploads/2010/03/defaultfeaturedimg.png"><?php } ?>
    </a>
    
    <!--Lead Post Text-->
    	<div id="lead-text">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
        <?php the_title(); ?></a> <span class="commentcount"> (<?php comments_popup_link('0', '1', '%'); ?>)</span></h2>
        <br /><p class="postmetadata"><?php _e('Categorized as','Mimbo'); ?> <?php the_category(',') ?>
    <br /><?php the_tags('Tagged as ',', ');?></p>
    
    	<?php the_excerpt(); ?><p align="right"><a href="<?php the_permalink() ?>" rel="bookmark" title="More">Read More</a></p>
    
    </div>
    <!--End Loop One--?
    <?php endwhile; ?>
    <!--End Lead or Sticky Post-->

    Not a php expert but can I modify this:

    <?php
             $blog_query = 'showposts=1&cat=-5&paged='.$paged;
    	$posts = query_posts($blog_query);
    	while (have_posts()) : the_post(); ?>

    to say OR if sticky is true?

    Thanks in advance.

    Actually in playing around I’ve discovered that a sticky post gets called first. So with the above code, if I have the latest post in category 4 and another post that is sticky, both posts appear at the top of the page with lead formatting.

    So is there a way to get that loop to return only one post, i.e. the most recent that post that is sticky or is not in category 5?

    Again, most grateful if anyone can help me.

    search in the www.ads-software.com codex pages for “template tag sticky”

    you’ll get all you need in there.

    ??

    In case others are looking for what I was looking for, this is what I came up with:

    <?php $args = array(
    	'posts_per_page' => 1,
    	'post__in'  => get_option('sticky_posts'),
    	'caller_get_posts' => 1,
    	'category__not_in' => array(5,151)
    );
    query_posts($args);?>
    	<?php while (have_posts()) : the_post(); $do_not_duplicate = $post->ID; ?>

    Pretty much copied from the Codex: Function Reference/Query Posts

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Show posts from multiple categories on homepage’ is closed to new replies.