• Resolved Mike

    (@belinep)


    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?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Adam Brown

    (@adamrbrown)

    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.

    Thread Starter Mike

    (@belinep)

    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…

    Adam Brown

    (@adamrbrown)

    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;
    ?>
    Thread Starter Mike

    (@belinep)

    OMG! That couldn’t be more perfect!

    Thank you OH so much!

    -MiKe-

    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Post Thumb Script’ is closed to new replies.