Title: Multiple categories, one query using $count
Last modified: August 19, 2016

---

# Multiple categories, one query using $count

 *  [VicePrez](https://wordpress.org/support/users/viceprez/)
 * (@viceprez)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/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.

Viewing 1 replies (of 1 total)

 *  Thread Starter [VicePrez](https://wordpress.org/support/users/viceprez/)
 * (@viceprez)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-one-query-using-count/#post-1952725)
 * Bump

Viewing 1 replies (of 1 total)

The topic ‘Multiple categories, one query using $count’ is closed to new replies.

## Tags

 * [count](https://wordpress.org/support/topic-tag/count/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [multiple categories](https://wordpress.org/support/topic-tag/multiple-categories/)

 * 1 reply
 * 1 participant
 * Last reply from: [VicePrez](https://wordpress.org/support/users/viceprez/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-one-query-using-count/#post-1952725)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
