WordPress.org

Support

Support » Themes and Templates » [Resolved] Function to get first image associated with current post

[Resolved] Function to get first image associated with current post

  • 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' />";
    				}
    				?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • 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(); ?>

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

    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 …. 🙂

    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! 🙂

    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(); ?>

    Sorry I was being stupid and didn’t even test your code, duh. Thanks! 🙂 🙂 🙂

    Great code!

    Works for me. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Function to get first image associated with current post’ is closed to new replies.