• greg9885

    (@greg9885)


    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)
  • Thread Starter greg9885

    (@greg9885)

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

    Thread Starter greg9885

    (@greg9885)

    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

    (@alchymyth)

    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

    (@greg9885)

    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

    (@alchymyth)

    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

    (@greg9885)

    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]

    Thread Starter greg9885

    (@greg9885)

    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

    Michael

    (@alchymyth)

    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

    (@greg9885)

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

    Michael

    (@alchymyth)

    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

    (@greg9885)

    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

    (@alchymyth)

    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

    (@greg9885)

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

    Michael

    (@alchymyth)

    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

    (@greg9885)

    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.

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

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