WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Post Thumb Script (6 posts)

  1. belinep
    Member
    Posted 6 years ago #

    Establishing info. I have Exec-PHP (to execute PHP in Text widgets), and a custom field "Image" for _certain_ posts.

    I am trying to get this code to work:

    <?php query_posts('cat=5,10'); ?>
    
    <?php while ($pt_count=>3) : the_post(); ?>
    
    <?php if get_post_custom_values("Image") { ?>
    
    <div class="thumbnails" style="text-align=center;">
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <img src="<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" />
    
    </a></div>
    
    <?php ; $pt_count++ } endwhile; ?>

    The idea is to display 3 thumbnails from the selected categories. I would have passed 'showposts=3' but if there isn't an image for the post, I don't want the code to be written, and I want to be sure that 3 display.

    Any ideas?

  2. Adam Brown
    Member
    Posted 6 years ago #

    You didn't say what the problem is. But just looking at your code, I would suggest these revisions to make it "prettier". By that, I mean two things: Easier to read, and less likely to cause parse errors:

    <?php 
    
    query_posts('cat=5,10');
    
    while ($pt_count<=3) :
    
     the_post();
    
     if ( get_post_custom_values("Image") ) { ?>
    
      <div class="thumbnails" style="text-align=center;">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <img src="<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" />
        </a>
      </div>
    
      <?php
      $pt_count++;
     }
    endwhile;
    ?>

    Edit: After prettifying, I noticed some errors in your code. Try it the way it looks now.

  3. belinep
    Member
    Posted 6 years ago #

    Something is happening, but it's not good (nothing was happening before).

    The page loads most of the way through, then binds up for about a minute, then when id does fully load, the page ends at the end of this loop...

    EDIT: It breaks all my pages...

  4. Adam Brown
    Member
    Posted 6 years ago #

    If the page freezes up, then it sounds like that while loop is never resolving. Something like this might be a more robust approach, since it won't cause an infinite loop:

    <?php 
    
    query_posts('cat=5,10');
    
    $pt_count = 0; // initialize counter
    
    if (have_posts()) : while(have_posts()) : the_post();
    
     if ( !($values = get_post_custom_values("Image") ) )
       continue; // go to next post
     // note that we defined $values already
    
     // display the image:
     ?>
      <div class="thumbnails" style="text-align=center;">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <img src="<?php echo $values[0]; ?>" alt="" />
        </a>
      </div>
     <?php
     $pt_count++;
    
     if (3 == $pt_count)
      break; // stop looping at 3 images
    
    endwhile;
    endif;
    ?>
  5. belinep
    Member
    Posted 6 years ago #

    OMG! That couldn't be more perfect!

    Thank you OH so much!

    -MiKe-

  6. boilr
    Member
    Posted 5 years ago #

    this is almost exactly what im looking for. i want to display the 10 more recent posts and to include the custom field "thumb" before each post title link in the sidebar.

    ive tried implementing the above, and it does pull the image files, but i cannot figure out how to make the "category" = the most recent posts?

    any help would be greatly appreciated!

Topic Closed

This topic has been closed to new replies.

About this Topic