logo

$8
How to retrieve ONLY the first thumbnail in list & Conditional IF

I have a dilemma. I have a loop where it lists the 5 most recent articles from a certain category, which is fine and dandy, but what I need is that the first article listed to show the thumbnail associated with that article. I don't need all five posts to have a thumbnail, justthe first one.

Here's the code:



$toppostsar = array(
'numberposts' => 5,
'tag' => featured,
'category' => $catlink->term_id );

$topposts = get_posts($toppostsar);

echo "</div>
<div class='a-foldout-right'>
<span>Featured Videos</span>
<div class='a-foldout-right-button'>
<div class='a-featured-img'>
<a href='#'></a>
</div>
<div class='a-foldout-right-button-contain-1'>
<a href='#'>Title of a featured article for this category.</a>
</div>
<div class='a-foldout-right-button-contain-2'>
<span>by&nbsp;</span><a href='#'>rare12</a><span>|&nbsp;500,000 views</span>
</div>
</div>";

foreach($topposts as $selpost) {

$linkid = $selpost->ID;

$selpermalink = get_post_permalink($linkid);

$selauthorid = $selpost->post_author;

$selauthorurl = get_author_posts_url($selauthorid);

$selauthordata = get_userdata($selauthorid);

echo"<div class='a-foldout-right-button'>
<div class='a-foldout-right-button-contain-1'>
<a href='" . $selpermalink . "'>" . $selpost->post_title . "</a>
</div>
<div class='a-foldout-right-button-contain-2'>
<span>by&nbsp;</span><a href='" . $selauthorurl . "'>" . $selauthordata->display_name . "</a><span>|&nbsp;500,000 views</span>
</div>
</div>";



This is my most pressing issue. I also have another small issue pertaining to conditional if statements.

I have another loop where it lists the 10 most recent posts throughout my site and what I want to have happen with this loop is show a small icon pic for only certain posts in a certain category.

For example, I have a video section on my site and for all posts within this loop that are coming from the video category, there would be a small "play" icon besides the title of the post. Here's the code for that:


<?php
global $post;
$postslist = get_posts('showposts=10');
foreach ($postslist as $post) :
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);
?>

<li>

<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'sidebar-mini' ); ?></a>
<?php } elseif($image) { ?>
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo $image; ?>&amp;w=43&amp;h=36&amp;zc=1" alt="" /></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo( 'template_url' ); ?>/images/no-image-75x50.jpg" width="43" height="36" alt="" align="left" /></a>
<?php } ?>

<?php if (is_post_type_hierarchical('videos')) {?>

Test image

<?php }?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <div style="clear:right;"></div>
<small><?php relative_post_the_date(); ?> </small>

</li>
<?php endforeach; ?>




I tried playing around with the code above several times by using "test image" as an holder to see if the text shows up which it doesn't.

Any help on these two things would be greatly appreciated.

Brennen Jones | 09/03/10 at 10:26pm | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    09/10/10
    7:43pm
    Nilesh shiragave says:

    Hi

    For the video play button just replace

    if(is_post_type_hierarchical('videos'))

    with

    if(in_category('videos'))



    • 09/03/10 11:46pm

      Brennen Jones says:

      Thanks! That did the trick. For some reason, I thought I had already tried that and it didn't work, lol. But it's all good now.

      Would you by any chance know how to fix the first part of my question?

    • 09/04/10 1:43am

      Nilesh shiragave says:

      let me know on first code where you want the image? i mean location in the html. send me your code of first error

  • avatar
    Last edited:
    09/10/10
    7:43pm
    Michelle Dancer says:

    For the first issue you can set some counter variable to 1 before your foreach loop ( $counter = 1;). At the very end of the foreach loop, before the closing }, put $counter++ which will add 1 to the existing counter value.

    Then wherever you want the thumbnail to show up within your foreach loop you can just wrap it in an if statement to check if $i = 1. As long as the check comes before the $counter++ whatever is inside your if statement should show on the first post and none after that.

    • 09/04/10 12:44am

      Brennen Jones says:

      Can you post example? I just tried what you said and it didn't work. I don't know if I'm wrapping everything correctly.

    • 09/04/10 4:34am

      Michelle Dancer says:

      Ok sorry it was about 6 in the morning so I was trying to get away with being lazy :D

      I've put plenty of comments in here, you should be able to make sense of it, if it doesn't work let us know what it's doing (specific error, white screen of death etc).

      <?php
      //set counter variable to 1 OUTSIDE of the foreach loop
      $counter = 1;

      //start foreach
      foreach($topposts as $selpost) {

      //your existing variables
      $linkid = $selpost->ID;
      $selpermalink = get_post_permalink($linkid);
      $selauthorid = $selpost->post_author;
      $selauthorurl = get_author_posts_url($selauthorid);
      $selauthordata = get_userdata($selauthorid);

      //only show the thumbnail if our counter is still set to 1
      if( $counter = 1 ) {
      $thumb = get_the_post_thumbnail();
      }
      //if counter is higher than 1, $thumb returns nothing
      else {
      $thumb = '';
      }
      //now just echo $thumb wherever you want it to show up in the markup, it won't output anything if the counter is higher than one. If you need a div or whatever around it you can just add that inside the if statement too.

      //generate the markup, as you were doing
      echo "<div class='a-foldout-right-button'>
      <div class='a-foldout-right-button-contain-1'>
      <a href='" . $selpermalink . "'>" . $selpost->post_title . "</a>
      </div>
      <div class='a-foldout-right-button-contain-2'>
      <span>by&nbsp;</span><a href='" . $selauthorurl . "'>" . $selauthordata->display_name . "</a><span>|&nbsp;500,000 views</span>
      </div>
      </div>";

      //increment the counter by one, ready for the next loopthrough
      $counter++;

      //end foreach (this closing tag isn't in your example code, I stuck it in for neatness)
      }
      ?>

    • 09/04/10 10:33am

      Brennen Jones says:

      Thanks for your time.

      I think we're moving in right direction. It's still not showing up. So I'm including code this time for you to check for me.

      	  $counter = 1;

      foreach($topposts as $selpost) {

      $linkid = $selpost->ID;

      $selpermalink = get_post_permalink($linkid);

      $selauthorid = $selpost->post_author;

      $selauthorurl = get_author_posts_url($selauthorid);

      $selauthordata = get_userdata($selauthorid);

      //only show the thumbnail if our counter is still set to 1

      if( $counter = 1 ) {

      $thumb = the_post_thumbnail( 'menu' );

      }

      //if counter is higher than 1, $thumb returns nothing

      else {

      $thumb = '';

      }



      echo"<div class='a-foldout-right-button'> $thumb
      <div class='a-foldout-right-button-contain-1'>
      <a href='" . $selpermalink . "'>" . $selpost->post_title . "</a>
      </div>
      <div class='a-foldout-right-button-contain-2'>
      <span>by&nbsp;</span><a href='" . $selauthorurl . "'>" . $selauthordata->display_name . "</a><span>|&nbsp;500,000 views</span>
      </div>
      </div>";

      //increment the counter by one, ready for the next loopthrough

      $counter++;


      }

      echo "</div></div></ul></li></ul>";

      }


      Am I do everything correctly?

    • 09/04/10 3:06pm

      Michelle Dancer says:

      You didn't say what behaviour you're getting but if it's a PHP error or white screen it might just be due to this line:

      echo"<div class='a-foldout-right-button'> $thumb


      Change that to

      echo"<div class='a-foldout-right-button'>" . $thumb . "


      and cross your fingers!

    • 09/04/10 8:25pm

      Brennen Jones says:

      Hello...

      and I'm sorry. I meant to say that the thumbnail isn't showing. Everything else is showing up like it was before but the thumbnail isn't showing even after I tried your revised copy above.

  • avatar
    Last edited:
    09/10/10
    7:43pm
    Peter Bouwmeester says:

    Brennan,
    I'm not sure why the thumb isnt showing up yet. The code you have created so far after Michelle her help should actually show a thumb for _all_ posts since there are two errors in there. To correct those:

    Change : if ( $counter = 1 )
    into : if ( $counter == 1 )

    and replace : the_post_thumbnail(..)
    by : get_the_post_thumbnail(..)

    Furthermore, are you using featured thumbnails for those posts? Obviously you cant display a thumb if you havent set it first..
    Good luck.

  • avatar
    Last edited:
    09/10/10
    7:43pm
    Tobias Nyholm says:


    foreach($topposts as $apost) {
    $thumb_image = get_the_post_thumbnail($apost->ID);
    break;
    }

    echo $thumb_image;

This question has expired.





Current status of this question: Completed