Title: Difficult Conditional Loop Problems
Last modified: August 19, 2016

---

# Difficult Conditional Loop Problems

 *  [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/)
 * I’ve been looking all over the forums and the web and I can’t figure out what
   to do. The loop is getting confusing because I’ve looked at it for so long.
 * Here’s what I’m trying to accomplish with my index.php
 * – **1 Featured Post** that only shows on first (home page) page which is styled
   completely different and has extra content than just a regular post (ie: can’t
   use “sticky” feature)
    – **8 More Posts.** Each will be tagged as 1 of three 
   things (so three different styles of posts). So I’d like the loop to say IF TAGGED
   THIS TYPE OF POST THEN SHOW THIS, IF TAGGED THIS TYPE OF POST THEN SHOW THIS,
   AND IF TAGGED THIS TYPE OF POST THEN SHOW THIS.
 * Here’s the code I’m workin’ with…
 *     ```
       <?php get_header(); ?>
   
       <?php if ( is_home() and !is_paged() ) { ?>
   
       <!--FEATURED -->
       <?php query_posts('tag=featured&showposts=1'); ?>
       <?php while (have_posts()) : the_post(); ?>
       <!--CONTENT -->
       <?php endwhile; ?>
       <!--END FEATURED -->
       <?php } ?>
   
       <!--LOOP TO GET 8 POSTS-->
       <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("offset=1&showposts=8&paged=$page"); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>    
   
       <!------------ I DON'T KNOW WHAT TO PUT HERE ------------>
   
       <?php endwhile; ?>
       <!--END LOOP TO GET 8 POSTS-->
   
       <?php else : ?>
   
       	<h2>Not Found</h2>
       	<p>Sorry, but you are looking for something that isn't here.</p>
       	<?php get_search_form(); ?>
   
       <?php endif; ?>
   
         </div>
   
       <?php get_footer(); ?>
       ```
   
 * Help would be greatly appreciated. Thank you!!

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/difficult-conditional-loop-problems/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/difficult-conditional-loop-problems/page/2/?output_format=md)

 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445049)
 * Anyone have any idea how to accomplish this? I’m really struggling over this.
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445182)
 * Am I not closing the loop correctly? I know I need some sort of conditional statement
   in there, but I’m not sure what to put in there??
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445187)
 * what have you so far?
 * if, with ‘tagged’ you mean ‘having a tag’ and just one ?
    then you could try 
   something conditional like:
 * from:
    `<!------------ I DON'T KNOW WHAT TO PUT HERE ------------>`
 * to:
 *     ```
       <?php
       $posttags = get_the_tags();
       if ($posttags) {
       $tagged = $posttags[0]->name;
       }
       if($tagged == 'type1') { show posts the way 1 }
       elseif($tagged == 'type2') { show posts the way 2 }
       elseif($tagged == 'type3') { show posts the way 3 }
       else { show standard }
       ?>
       ```
   
 * for more help, you would need to provide more details.
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445189)
 * By “tagged” I mean that if the post has a certain tag (ie: “orange”) then I’d
   like the posts to be displayed in a certain format.
 * I’m trying to use the code you gave me, but I’m not quite sure how. I’ve tried
   to put php content where you have “show posts the way 1” and I’m getting an error.
 * This is what I’m trying…
 *     ```
       <?php
       $posttags = get_the_tags();
       if ($posttags) {
       $tagged = $posttags[0]->name;
       }
       if($tagged == 'articles') { <a href="<?php the_permalink() ?>"><?php the_title(); ?></a> }
       elseif($tagged == 'type2') { <a href="<?php the_permalink() ?>"><?php the_title(); ?></a> }
       elseif($tagged == 'type3') { <a href="<?php the_permalink() ?>"><?php the_title(); ?></a> }
       else { show standard }
       ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445191)
 * wrong order of opening/closing php tags:
 * corrected:
 *     ```
       <?php
       $posttags = get_the_tags();
       if ($posttags) {
       $tagged = $posttags[0]->name;
       }
       if($tagged == 'articles') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif($tagged == 'type2') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif($tagged == 'type3') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       else { /*show standard*/ }
       ?>
       ```
   
 * hope this works, good luck
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445194)
 * Hmm… I’m still not getting it. Nothing is showing up at all. Here’s my whole 
   code in index.php
 * _[Code moderated as per the [Forum Rules](http://wordpress.org/support/topic/68664).
   Please use the [pastebin](http://wordpress.pastebin.ca/)]_
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445248)
 * Ok, my code got taken off. I didn’t know I couldn’t paste it. I still can’t get
   anything to show so here’s my index.php…
 * [http://wordpress.pastebin.com/6vWQQQwn](http://wordpress.pastebin.com/6vWQQQwn)
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445253)
 * my bad, i got the code wrong to get the first tag name from the post.
 * hope this works better:
 *     ```
       <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("offset=1&showposts=8&paged=$page"); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); 
   
       $posttags = get_the_tags();
       $count=0; $tagged = '';
       if ($posttags) {
       foreach($posttags as $tag) {
       if (0 == $count) {
       $tagged = $tag->name; $count++;
       }
       }
       }
   
       if($tagged == 'articles') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif ($tagged == 'videos') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif($tagged == 'links') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       else { /*show standard*/ };
   
       endwhile; ?>
       ```
   
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445254)
 * Hmm… still nothing showing up. This is what my whole index.php looks like: [http://wordpress.pastebin.com/qWjg8Q1w](http://wordpress.pastebin.com/qWjg8Q1w).
   Am I displaying is correctly?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445258)
 * try to add `<?php wp_reset_query(); ?>`
 * before this line:
 *     ```
       <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("offset=1&showposts=8&paged=$page"); ?>
       ```
   
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445261)
 * Nope, still doesn’t work. And just in case anyone asks, I’m positive that I have
   the posts tagged correctly.
 * Do I need to use some sort of query_posts function?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445263)
 * do you have any plugins activated?
 * try deactivating all plugins and see if the problem persists.
 * if this solves the problem, re-activate one plugin at the time and see if one
   plugin triggers the problem again.
 * also, if you could provide a link to your site, there might be some clues in 
   the html code from the browser.
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445265)
 * Plugins aren’t the issue. I just deactivated all of them and still nothing. Here’s
   the [link to my site](http://www.test.gregorydougherty.com/).
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445270)
 * last try – as i could see that you use a capital letter at the beginning of your
   tag names:
 *     ```
       if($tagged == 'Articles') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif ($tagged == 'Videos') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       elseif($tagged == 'Links') { ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><?php }
       else { /*show standard*/ };
       ```
   
 *  Thread Starter [greg9885](https://wordpress.org/support/users/greg9885/)
 * (@greg9885)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/#post-1445271)
 * Holy Crap!!!!! That works!!!! Thank you so so much!!!! This was such a huge help,
   you have no idea.
 * One quick question though… Do you know how I would go about adding a specific
   class to every 4th post?
 * The way I’m having the posts display, there’s extra padding on the 4th post that
   I’d like to get rid of. I’ve been messing around with the code from [this topic](http://wordpress.org/support/topic/365371?replies=4),
   but I can’t seem to get it with the code that I have.
 * Would you mind taking a look, it might be something easy. I’m pretty sure it 
   has something to do with the counter.

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/difficult-conditional-loop-problems/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/difficult-conditional-loop-problems/page/2/?output_format=md)

The topic ‘Difficult Conditional Loop Problems’ is closed to new replies.

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [the-loop](https://wordpress.org/support/topic-tag/the-loop/)

 * 20 replies
 * 2 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/difficult-conditional-loop-problems/page/2/#post-1445298)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
