Forums

Difficult Conditional Loop Problems (21 posts)

  1. greg9885
    Member
    Posted 2 years ago #

    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!!

  2. greg9885
    Member
    Posted 2 years ago #

    Anyone have any idea how to accomplish this? I'm really struggling over this.

  3. greg9885
    Member
    Posted 2 years ago #

    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??

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

    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.

  5. greg9885
    Member
    Posted 2 years ago #

    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 }
    ?>
  6. alchymyth
    The Sweeper
    Posted 2 years ago #

    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

  7. greg9885
    Member
    Posted 2 years ago #

    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. Please use the pastebin]

  8. greg9885
    Member
    Posted 2 years ago #

    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

  9. alchymyth
    The Sweeper
    Posted 2 years ago #

    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; ?>
  10. greg9885
    Member
    Posted 2 years ago #

    Hmm... still nothing showing up. This is what my whole index.php looks like: http://wordpress.pastebin.com/qWjg8Q1w. Am I displaying is correctly?

  11. alchymyth
    The Sweeper
    Posted 2 years ago #

    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"); ?>
  12. greg9885
    Member
    Posted 2 years ago #

    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?

  13. alchymyth
    The Sweeper
    Posted 2 years ago #

    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.

  14. greg9885
    Member
    Posted 2 years ago #

    Plugins aren't the issue. I just deactivated all of them and still nothing. Here's the link to my site.

  15. alchymyth
    The Sweeper
    Posted 2 years ago #

    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*/ };
  16. greg9885
    Member
    Posted 2 years ago #

    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, 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.

  17. alchymyth
    The Sweeper
    Posted 2 years ago #

    could you be more specific?
    is that on the front page? i can't identify any post with extra padding there.

    the following code should give every fourth post a extra class:

    <?php $counter=0;
    ...query_posts...if you need it...
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <?php $counter++; ?>
    <div class="post <?php if( $counter%4 == 0 ) { echo ' post_four'; ?>">
    ...the usual html/php for post title, content, postmetadata etc. ...
    </div> <!--end of .post -->
    
    <?php endwhile; endif; ?>

    (untested)

  18. greg9885
    Member
    Posted 2 years ago #

    Yes this is for the front page and the "next page" as well (also known as page 2). I tried implementing the code you gave me, but I don't think it worked. Where should I put it?

  19. greg9885
    Member
    Posted 2 years ago #

    Ok, I've been trying to implement this with the code that works above but I'm having no luck at all. Also, I don't know if it will display correctly anyway, because I'd like to have every 4th post be styled differently. So, I'd like the 4th, 8th, 12th, etc styled with no padding. You can see what I mean here.

  20. greg9885
    Member
    Posted 2 years ago #

    Ohh man, I just organized the post titles and realized that my posts are getting messed up between pages as well.

    Here's the code I'm using: http://wordpress.pastebin.com/e4NQJUxP. Here's where the sites located.

    If anyone can help out with this, I'd owe you a beer or thirty.

  21. alchymyth
    The Sweeper
    Posted 2 years ago #

    best to start a new thread with this new challenge - maybe someone will be able to find a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic