WordPress.org

Forums

[resolved] Function to get first image associated with current post (8 posts)

  1. 3stripe
    Member
    Posted 6 years ago #

    Hello hello,

    This snippet works in my page template (which has multiple loops) but I want to convert it to a function... when I try to use it via functions.php it displays the same image for every post...

    So I guess the post id is getting messed up, but don't know how to fix it?

    Thanks

    <?php
    				$files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image");
    				if($files){
    					$keys = array_reverse(array_keys($files));
    					$j=0;
    						$num = $keys[$j];
    						$image=wp_get_attachment_image($num, 'large', false);
    						$imagepieces = explode('"', $image);
    						$imagepath = $imagepieces[1];
    							$thumb=wp_get_attachment_thumb_url($num);
    							print "<img src='$thumb' class='thumbnail' />";
    				}
    				?>
  2. t31os
    Member
    Posted 6 years ago #

    So this is what you did, correct?

    In functions file..

    <?php
    function somefunctionname() {
    
      $files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image");
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', false);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $thumb=wp_get_attachment_thumb_url($num);
        print "<img src='$thumb' class='thumbnail' />";
      endif;
    
    }
    ?>

    Then where you'd normally have your PHP you call the function instead...

    <?php somefunctionname(); ?>

  3. 3stripe
    Member
    Posted 6 years ago #

    Yes, cheers, I should have said that. This is all inside the loop by the way...

  4. t31os
    Member
    Posted 6 years ago #

    This works inside the loop as a function...

    function test_me() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', false);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $thumb=wp_get_attachment_thumb_url($num);
        print "<img src='$thumb' class='thumbnail' />";
      endif;
    }

    Obviously name it to something other then test_me .... :)

  5. 3stripe
    Member
    Posted 6 years ago #

    Thanks but I'm trying to simplify my template and move the code to a function, because I'm repeating it 3 or 4 times for each page! :)

  6. t31os
    Member
    Posted 6 years ago #

    The above is an example of it working as a function...

    What's the problem? :)

    This goes into your function file..

    function test_me() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', false);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $thumb=wp_get_attachment_thumb_url($num);
        print "<img src='$thumb' class='thumbnail' />";
      endif;
    }

    Then call it in your template...

    <?php test_me(); ?>

  7. 3stripe
    Member
    Posted 6 years ago #

    Sorry I was being stupid and didn't even test your code, duh. Thanks! :) :) :)

  8. wp-rory
    Member
    Posted 6 years ago #

    Great code!

    Works for me. :)

Topic Closed

This topic has been closed to new replies.

About this Topic