Multiple categories, one query using $count
-
hey guys,
im having trouble with a piece of code ive been working on. What im trying to do is grab 1 post from all the categories in my database and post it on my archives. The only trouble im experiencing is with css styling. Ive indicated where im having trouble below.
<?php //for categories 1,3,4,5,6,7 show 3 posts $cat_args=array( 'include' => '1,3,4,5,6,7', 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'showposts' => 1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 );?> <?php $posts=get_posts($args); static $count = 0; if ($posts) { foreach($posts as $post) { $count++; setup_postdata($post); ?> //TROUBLE STARTS HERE <?php if ($count <= 3):?> <div id="first-column"> <?php include (TEMPLATEPATH . '/custom-temp/entry-box.php' ); ?> </div> <?php elseif ($count > 3):?> <div id="second-column"> <?php include (TEMPLATEPATH . '/custom-temp/entry-box.php' ); ?> </div> <?php endif; ?> <?php } // foreach($posts } // if ($posts } // foreach($categories ?>
What happens is that when i wrap my “css styling template, entry-box.php” with the following divs:
<?php if ($count <= 3):?> <div id="first-column"> <?php include (TEMPLATEPATH . '/custom-temp/entry-box.php' ); ?> </div> <?php elseif ($count > 3):?> <div id="second-column"> <?php include (TEMPLATEPATH . '/custom-temp/entry-box.php' ); ?> </div>
It wraps each individual post (with either the first-column, or second-column div, instead of “3” of the posts. To illustrate, say i outputted 6 posts, 1 from each category, im trying to wrap 3 of those posts into a column and the other three into a column parallel to it. Instead each individual post is being wrapped with the div i gave it.
Plus if u guys see anything wrong with my code, or notice things i can do to clean it up please let me know. Its my first time working with php and wordpress.
Thanks in advance.
- The topic ‘Multiple categories, one query using $count’ is closed to new replies.