WordPress.org

Ready to get started?Download WordPress

Forums

Help me troubleshoot short Loop. It works, almost. (7 posts)

  1. freshyill
    Member
    Posted 8 years ago #

    I think this is *mostly* right. It should give me three boxes containing some custom fields. The first two are fine, but the third one ends up really weird. I think this is a simple matter of getting things in the right place.

    Here's the code:

    <div id="promos" class="clearfix">
    <?php if (have_posts("category=22&number=3")) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="promoitem">
    <?php echo get_post_meta($post->ID, 'Feature promo image', true); ?>
    <h2><?php echo get_post_meta($post->ID, 'Feature promo header', true); ?></h2>
    <p><?php echo get_post_meta($post->ID, 'Feature promo text', true); ?></p>
    </div>
    <?php endwhile; ?>

    </div>
    <?php endif; ?>

    And here's the output:

    <div id="promos" class="clearfix">
    <div class="promoitem">
    <img src="/images/grohl_72.jpg" alt="Grohl" /><h2>Foo Fighters rock Va.</h2>
    <p>This is text this is text this is text this is text lorem ipsum dolor sit amet This is text this is text this is text this is text lorem ipsum dolor sit ame ipsum dolor.</p>
    </div>
    <div class="promoitem">
    <img src="/images/demondays.jpg" alt="Gorillaz"<h2>New Gorillaz album</h2>
    <p>This is text this is text this is text this is text lorem ipsum dolor sit amet This is text this is text this is text this is text lorem ipsum dolor sit ame ipsum dolor.</p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>
    <div class="promoitem">
    <img src="/images/deltron3030.jpg" alt="Deltron 3030" /><h2>Automator is God</h2>
    <p>This is text this is text this is text this is text lorem ipsum dolor sit amet This is text this is text this is text this is text lorem ipsum dolor sit ame ipsum dolor.</p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>
    <div class="promoitem">
    <h2></h2>
    <p></p>
    </div>

    </div>

  2. jaseone
    Member
    Posted 8 years ago #

    You need to filter:

    <?php while (have_posts()) : the_post(); ?>;
    like:
    <?php while (have_posts("category=22&number=3")) : the_post(); ?&>

    EDIT: ARGH screwed up my backquotes, hopefully that is right now.

  3. freshyill
    Member
    Posted 8 years ago #

    I had to take out the & at the end to get it to work, but it gives me the same result that I was getting before. Still no luck.

    While I'm at it, I should mention that this prevents the next loop on the page from working. It gives me the not found in the else.

  4. freshyill
    Member
    Posted 8 years ago #

    OK, I think I figured something out. This isn't limiting the category to 22 and the number to the last 3, like I thought it was. This is showing *all* entries. The empty ones don't have these fields.

    How can I change it to show only the last 3 entries in category 22?

  5. freshyill
    Member
    Posted 8 years ago #

    I think I've got it. This is the code I used. Can somebody tell me if this is the right way to do what I'm trying to do, or if there's a better method I might want to try?

    <div id="promos" class="clearfix">
    <?php $posts = get_posts( "category=22&number=3" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <div class="promoitem">
    <?php echo get_post_meta($post->ID, 'Feature promo image', true); ?>
    <h2><?php echo get_post_meta($post->ID, 'Feature promo header', true); ?></h2>
    <p><?php echo get_post_meta($post->ID, 'Feature promo text', true); ?></p>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
    </div>

  6. freshyill
    Member
    Posted 8 years ago #

    OK, still not complete.... This is causing errors on a loop later on the page. When this is in place, It only shows the last three entries, and for some reason, they all take me to the same article, of the most recent one, even when the URL up top is different.

    So I've definitely got some problem in this code. Come on, somebody here has to know something about WordPress.

  7. freshyill
    Member
    Posted 8 years ago #

    Anybody?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.