WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Assistance with Loop and PHP (3 posts)

  1. jbarryweb
    Member
    Posted 5 months ago #

    I'm using this loop in an attempt to get three posts that have images. I believe the issue has something to do with the way I'm incrementing $varz or the way the While statement is setup. Using this code currently returns a blank page with no content. Any help is much appreciated.

    <div class="featured-sidebar first">
        <?php $varz = 1 ?>
        <?php query_posts( 'cat=32,45,46,43,44,104,23,31,36&posts_per_page=20') ?>
        <?php if ( have_posts() ) : while ( $varz < 4 ) : the_post(); ?>
            <?php if ( has_post_thumbnail() ) {?>
                <?php if ($varz = 1){
                            echo"<img class=\"most-pop-overlay1\" src=";print IMAGES;echo"/images/one-overlay.png\" />"; }
                      elseif ($varz = 2){
                            echo"<img class=\"most-pop-overlay2\" src=";print IMAGES;echo"/images/two-overlay.png\" />"; }
                      elseif ($varz = 3){
                            echo"<img class=\"most-pop-overlay3\" src=";print IMAGES;echo"/images/three-overlay.png\" />"; }
                      else{}
                ?>
                <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
                    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(150,150)); ?></a>
                    <h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
                    <p>
                        <?php $exc = strip_tags(get_the_excerpt()); ?>
                        <?php echo string_limit_words($exc,12); ?>...
                    </p>
                <?php $varz++; ?>
            <?php } ?>
        <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_query() ?>
    </div>
  2. vtxyzzy
    Member
    Posted 5 months ago #

    You must use while (have_posts()) : in the loop instead of while ( $varz < 4 ).

    To break out of the while loop, change this:

    else{}

    to this:

    else{ break; }
  3. jbarryweb
    Member
    Posted 5 months ago #

    Thanks Mac, that did the trick.

Reply

You must log in to post.

About this Topic