Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What I'm trying to do on my homepage is to pull the latest post from each of 6 categories but then I need the variable $counter to increase (+1) for each post so that I can style them properly.

I've managed to display the latest post from each category in the right order, but by doing that I can't get $counter to work properly.

Any help would be greatly appreciated - thanks!

<?php get_header(); ?>

    <div class="col1">

        <?php 
             if (get_option('woo_show_carousel') == 'true' AND !is_paged()) { include (TEMPLATEPATH . "/includes/featured.php");}
        ?>

        <?php 
        if (is_paged()) $is_paged = true;

        if (get_option('woo_slider_exclude') == "true") $shownslides = get_option('woo_shown_slides');
        if(empty($showslides) || get_option('woo_show_carousel') == 'false'){
            $showslides = array();
        }  

         $showvideo = get_option('woo_show_video');
         $vidtags = get_option('woo_video_tags');
         if(!empty($vidtags)) {
            $vidtags_array = explode(',',$vidtags);
         } else {
             $vidtags_array = array();
         }

         global $wpdb;
         foreach ($vidtags_array as $tag_name){
             $tag_name = trim($tag_name);
             $tagid = $wpdb->get_var("SELECT term_ID FROM $wpdb->terms WHERE name='$tag_name'");
             $tagids[] = $tagid;  
         }                
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

            $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
         'include' => '3,4,5,6,11,14',
         'orderby' => 'ID',
         'order' => 'ASC'
        );
            $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
    foreach( $terms as $term ) {
            $args = array(
                    "$param_type" => array($term->term_id),
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 1,
            'caller_get_posts'=> 1,
                'post__not_in' => $shownslides,
                'tag__not_in' => $tagids,
                'paged'=> $paged
                );
         query_posts($args);


         $home_content = get_option('woo_home_content');
         $home_boxes = get_option('woo_home_boxes');

         if (have_posts()) : 

         $full = FALSE;

         $width = get_option('woo_full_thumb_width');
         $height = get_option('woo_full_thumb_height');

         if( $home_boxes == 'On') { $small = TRUE; }
         elseif ($home_boxes == 'Off') { $full = TRUE; } 
         else {  $large_posts = intval($home_boxes); }

         $counter = 0; 
         while (have_posts()) : the_post(); 
            $counter++;
            $post_class = 'full'; 
            if( ($counter > $large_posts OR $small == TRUE) AND $full ==  FALSE)
            {
                $width = get_option('woo_boxed_thumb_width');
                $height = get_option('woo_boxed_thumb_height');
                if( $small == FALSE ) $counter = 1;
                $small = true;
                    if ($counter%2 == 0 ) { $post_class = 'fr';  } 
                    else { $post_class = 'fl'; }
            }

         ?> 
        <div class="post-outer post <?php echo $post_class; ?>">

            <div class="post-inner post-alt">       

                <?php woo_get_image('image',$width,$height); ?> 

                <h2><a title="<?php _e('Permalink to ',woothemes); ?><?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                <p class="category-head cufon"><?php the_category(', ') ?></p>


                <?php if ($home_content == 'false') { ?>
                    <p><?php echo strip_tags(get_the_excerpt(), '<a><strong>'); ?></p>

                <?php } else { ?>
                    <?php the_content(__('Continue Reading',woothemes)); ?>                     
                <?php } ?>            

                </div><!--/post-inner-->
            </div><!--/post-->

            <?php if ($counter%2 == 0 AND $small == TRUE ) { echo '<div style="clear:both;"></div>'; } ?>

        <?php endwhile; ?>
        <?php endif; 
        }
        }
        wp_reset_query(); 
        ?>

        <div class="more_entries">
            <?php if (function_exists('wp_pagenavi')) wp_pagenavi(); else { ?>
                <div class="fl"><?php previous_posts_link(__('&laquo; Newer Entries ',woothemes)) ?></div>
                <div class="fr"><?php next_posts_link(__(' Older Entries &raquo;',woothemes)) ?></div>
                <br class="fix" />
            <?php } ?>
        </div>      

        <div class="fix"></div>


        <?php 
            if (get_option('woo_show_video') == 'true') { include (TEMPLATEPATH . "/includes/video.php");}
        ?>

        <?php 
            if (get_option('woo_show_talking_points') == 'true') { include (TEMPLATEPATH . "/includes/talking-points.php");}
        ?>

    </div><!--/col1-->

share|improve this question
    
You have a lot of stuff going on there. I think you need to put your $counter = 0; above if (have_posts()) : –  Chris_O Apr 5 '11 at 3:10
    
Chris, I deleted my last comment and fixed the code below because I had made a mistake that I hadn't noticed. Moving the '$counter = 0;' didn't have any effect on its own, but changing it to 'static $counter = 0;' and moving fixed it completely. Thank you so much! –  Tim Apr 6 '11 at 15:45
add comment

1 Answer 1

Well after a good bit of searching I seem to have this working, I hope this helps someone.

    <div class="col1">

        <?php 
             if (get_option('woo_show_carousel') == 'true' AND !is_paged()) { include (TEMPLATEPATH . "/includes/featured.php");}
        ?>

        <?php 
        if (is_paged()) $is_paged = true;

        if (get_option('woo_slider_exclude') == "true") $shownslides = get_option('woo_shown_slides');
        if(empty($showslides) || get_option('woo_show_carousel') == 'false'){
            $showslides = array();
        }  

         $showvideo = get_option('woo_show_video');
         $vidtags = get_option('woo_video_tags');
         if(!empty($vidtags)) {
            $vidtags_array = explode(',',$vidtags);
         } else {
             $vidtags_array = array();
         }

         global $wpdb;
         foreach ($vidtags_array as $tag_name){
             $tag_name = trim($tag_name);
             $tagid = $wpdb->get_var("SELECT term_ID FROM $wpdb->terms WHERE name='$tag_name'");
             $tagids[] = $tagid;  
         }                
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

            $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
         'include' => '3,4,5,6,11,14',
         'orderby' => 'ID',
         'order' => 'ASC'
        );
            $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
    foreach( $terms as $term ) {
            $args = array(
                    "$param_type" => array($term->term_id),
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 1,
            'caller_get_posts'=> 1,
                'post__not_in' => $shownslides,
                'tag__not_in' => $tagids,
                'paged'=> $paged
                );
         query_posts($args);


         $home_content = get_option('woo_home_content');
         $home_boxes = get_option('woo_home_boxes');

         static $counter = 0; 

         if (have_posts()) : 

         $full = FALSE;

         $width = get_option('woo_full_thumb_width');
         $height = get_option('woo_full_thumb_height');

         if( $home_boxes == 'On') { $small = TRUE; }
         elseif ($home_boxes == 'Off') { $full = TRUE; } 
         else {  $large_posts = intval($home_boxes); }

         while (have_posts()) : the_post();

        $counter++;
            $post_class = 'full'; 
            if( ($counter > $large_posts OR $small == TRUE) AND $full ==  FALSE)
            {
                $width = get_option('woo_boxed_thumb_width');
                $height = get_option('woo_boxed_thumb_height');
                if( $small == FALSE ) $counter = 1;
                $small = true;
                    if ($counter%2 == 0 ) { $post_class = 'fr';  } 
                    else { $post_class = 'fl'; }
            }

         ?> 
        <div class="post-outer post <?php echo $post_class; ?>">

            <div class="post-inner post-alt">       

                <?php woo_get_image('image',$width,$height); ?> 

                <h2><a title="<?php _e('Permalink to ',woothemes); ?><?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                <p class="category-head cufon"><?php the_category(', ') ?></p>


                <?php if ($home_content == 'false') { ?>
                    <p><?php echo strip_tags(get_the_excerpt(), '<a><strong>'); ?></p>

                <?php } else { ?>
                    <?php the_content(__('Continue Reading',woothemes)); ?>                     
                <?php } ?>            

                </div><!--/post-inner-->
            </div><!--/post-->

            <?php if ($counter%2 == 0 AND $small == TRUE ) { echo '<div style="clear:both;"></div>'; } ?>

        <?php endwhile; ?>
        <?php endif; 
        }
        }
        wp_reset_query(); 
        ?>

        <div class="more_entries">
            <?php if (function_exists('wp_pagenavi')) wp_pagenavi(); else { ?>
                <div class="fl"><?php previous_posts_link(__('&laquo; Newer Entries ',woothemes)) ?></div>
                <div class="fr"><?php next_posts_link(__(' Older Entries &raquo;',woothemes)) ?></div>
                <br class="fix" />
            <?php } ?>
        </div>      

        <div class="fix"></div>


        <?php 
            if (get_option('woo_show_video') == 'true') { include (TEMPLATEPATH . "/includes/video.php");}
        ?>

        <?php 
            if (get_option('woo_show_talking_points') == 'true') { include (TEMPLATEPATH . "/includes/talking-points.php");}
        ?>

    </div><!--/col1-->

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.