Title: Calculate div width from post count?
Last modified: August 19, 2016

---

# Calculate div width from post count?

 *  Resolved [fhoto](https://wordpress.org/support/users/fhoto/)
 * (@fhoto)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/calculate-div-width-from-post-count/)
 * Hello!
 * Trying to get a horizontal layout working for a new portfolio site, where each
   different story is in its own category and gets a page of its own. Problem being
   the horizontal container must have a fix width corresponding to the number of
   posts within the shown category.
    This might be trivial, but I haven’t quite 
   got the hang of php yet. I’m guessing it’s possible to calculate the div width
   by creating a variable which multiplies the post count in the shown category 
   with the post width. But how?
 * Any ideas? There’s a test site up at [http://www.fhoto.se/nytest](http://www.fhoto.se/nytest)
 * Would really appreciate help/advice/ideas! Thanks!
 * Fred

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/calculate-div-width-from-post-count/#post-829330)
 * It’s a hack, but how about two loops? The first one gives the count, which is
   passed on to the second ‘real’ loop?
 *     ```
       <?php $count=1; ?>       // before the first loop
       <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
       <php $count++;?>
       <?php endwhile; ?>
       <?php else : ?>
       <?php endif; ?>
       ```
   
 *     ```
       <?php $total=$count*100; ?>     //within the second loop
       <div style="width:<?php echo "$total"; ?>px;">
       blah
       </div>
       ```
   
 * Which might output this:
 *     ```
       <div style="width:100px;">
       blah
       </div>
       ```
   
 * If I understand your question correctly 🙂
 *  Thread Starter [fhoto](https://wordpress.org/support/users/fhoto/)
 * (@fhoto)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/calculate-div-width-from-post-count/#post-829346)
 * Great! Don’t know if it’s the best way to fetch the post count, but it works 
   a charm. Here’s what I used:
 *     ```
       <?php $count=0; ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <?php if ( in_category('6') && is_home() ) continue; ?>
       <?php $count++;?>
       <?php endwhile; ?>
       <?php else : ?>
       <?php endif; ?>
   
       <?php $total=$count*670; ?>
       <div id="bilder" style="width:<?php echo "$total"; ?>px;">
   
       //second loop here
       ```
   
 * Excluded one category, got to test it some more once I get more of the site going.
   But as for now, this seems to do the trick. Already have two loops on the page
   though and I don’t know if this will interfere, but we’ll see.
 * Thanks!
 *  [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/calculate-div-width-from-post-count/#post-829414)
 * just put the standard query_post in front of each loop, that’s all you need to
   keep everything happy
 * 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Calculate div width from post count?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [richarduk](https://wordpress.org/support/users/richarduk/)
 * Last activity: [17 years, 9 months ago](https://wordpress.org/support/topic/calculate-div-width-from-post-count/#post-829414)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
