• Hi, I’m new to wordpress, and have quickly got stumped…i’m coming from HTML/CSS only and PHP is a challenge : /

    I have imported and adapted an existing (non-wordpress) site, and I have a super nice slider that I am trying to call each of my post images into a ‘<div>’ individually (I am not using the ‘ul’ ‘li’ model for a slider).

    (i’m sure I can get a slider plugin, but I need to stay true to my existing design…and in general would like to learn this).

    I found the following code that seems to work in part – but consistently shows only the first image, whether I choose ‘1’, ‘2’, or ‘3’ etc

    I have this in my function.php:

    function getImage($num) {
    global $more;
    $more = 1;
    $link = get_permalink();
    $content = get_the_content();
    $count = substr_count($content, ‘<img’);
    $start = 0;
    for($i=1;$i<=$count;$i++) {
    $imgBeg = strpos($content, ‘<img’, $start);
    $post = substr($content, $imgBeg);
    $imgEnd = strpos($post, ‘>’);
    $postOutput = substr($post, 0, $imgEnd+1);
    $postOutput = preg_replace(‘/width=”([0-9]*)” height=”([0-9]*)”/’, ”,$postOutput);;
    $image[$i] = $postOutput;
    $start=$imgEnd+1;
    }
    if(stristr($image[$num],'<img’)) { echo ‘‘.$image[$num].”“; }
    $more = 0;
    }

    and this in single.php within the loop:
    getImage(‘2’);

    It seems like there must be a simple solution – it calls the first image…but the first image only…what do I need to adapt to get each image?

    Help much, much appreciated!!!!
    (ps I am enjoying WordPress land after many years of being scared to put my toe in)

    • This topic was modified 8 years, 4 months ago by emergingideas. Reason: because i dunno how to write the code bits with out messing the formatting
    • This topic was modified 8 years, 4 months ago by emergingideas.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That’s certainly the long way around. There’s a function for that

    https://codex.www.ads-software.com/Function_Reference/get_attached_media

    Thread Starter emergingideas

    (@emergingideas)

    Thanks Steve…i’m learning slowly!

    Could you help me a little more? – how can I use this function for getting specific images from my post – like ‘image 1’ for example…

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Something like

    $images = get_attached_media( 'image' );
    foreach ($images as $i) {
        print_r( $i);
    }

    That will echo out the image objects; you can decide which elements you want to use.

    Thread Starter emergingideas

    (@emergingideas)

    thanks…this makes sense…but i’m still not getting any joy. Slow learning curve.

    what would the echo look like? and how it is integrated? Something like this?

    img src = echo $image->url()

    and can I put the whole shibang in a div do you think?

    Moderator bcworkz

    (@bcworkz)

    There are a couple caveats with get_attached_media(). The media needs to have been added through the media library. If you simply added an image by hand coding the img tag, then there is not going to be any related attachment object for that image. Same goes for images uploaded for a different post and reused for the current post in question. That image’s attachment object is going to be for the post it was originally inserted into, not the one it was reused in. Once again, no attachment object will exist for the current post.

    So unless all of your images were newly inserted for the current post, you are left with searching through the post content for img tags like you have done. While I’m not immediately seeing why your code is only returning the first found image, I think your basic approach should work, there’s just a small bug to quash somewhere. Learning to debug PHP is a good skill to develop. The most basic and useful technique is to simply echo out (or print_r() or var_dump() in the case of arrays and objects) particular variables at strategic locations. If you do this a few times, you should be able to zero in on where the glitch lies.

    Welcome to the WP community!

    Thread Starter emergingideas

    (@emergingideas)

    Thanks @bcworkz I will try!

    Thread Starter emergingideas

    (@emergingideas)

    Thanks for your help here guys…but I am still struggling..

    Could you help me get on the right path, or lay it out for me very simply (forgive me for being a numpty)

    I have placed this in like this:

    <div class="w-slide">
    
    <?php $images = get_attached_media( 'image' );
    foreach ($images as $i) {
        print_r( $i);
    } ?>
    
    <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt']; ?>">
    
    </div>

    I’m sure I am missing some v.important bits…my images are not displaying at all
    (and I re-uploaded them to the post after using the get_attached_media…thanks for your tip on this)

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The print_r is just for your own debugging. It should not be part of the final code. Also, the extraction of images needs to be part of the foreach.

    Moderator bcworkz

    (@bcworkz)

    Did you place your most recent coding effort within the Loop of your theme’s single.php? If so, that’s OK for now, but you should some time soon create a child theme to contain your theme edits so they are not lost when the theme is updated.

    When your code ran, you should have gotten a WP_Post Object listing for each image in the post. (BTW, you’ll get a nicer output if it is inside < pre> < /pre> tags, or you could just look at the HTML source view) Each listing is all the attachment properties for each image, the property key in [square brackets] followed by the assigned value after the =>.

    You may be tempted to use the “guid” property to construct an img tag, but don’t do that. The GUID cannot officially be relied on to have a valid URL even though it’s invariably the case. The data you need is actually in post meta. You can directly view DB data such as this with phpMyAdmin, which is usually accessed through your hosting account’s cPanel, Plesk, or similar interface. phpMyAdmin is a very powerful program, which means you can easily scramble your entire DB if you try to change something without knowing what you’re doing. What I mean to say is just look, don’t try to edit or change anything!

    You don’t really even need to look, except as an educational experience. You can get the needed meta data without a low level function like get_post_meta(). In this case you can use wp_get_attachment_image_src(), which returns all you need to construct an img tag.

    You could then replace the print_r( $i ); with this for example:

    $image_attributes = wp_get_attachment_image_src( $i->ID, "large" );
    if ( $image_attributes ) {
        echo "<img src=\"$image_attributes[0]\" width=\"$image_attributes[1]\" height=\"$image_attributes[2]\" />";
    }

    This will output all attached images in the registered “Large” size. If you are unfamiliar with PHP’s ability to automatically expand variables within double quoted strings, it’s worth reading up on this very useful feature.

    Your original example was intended to only output the specified nth image. You can achieve this by getting rid of the foreach loop and assigning the nth object to $i, something like $i = $images[ $num ];

    I don’t think the order of attachments necessarily reflects the order in which they occur in content, I believe they are ordered the same as in the DB, typically in the order the images were inserted. Much of the time the order will be the same as in content, but you cannot expect it to always be the case.

    So much to learn, yes? Hang in there, it’ll start making more sense with a little more experience ?? And don’t worry about your skill level, we are here to help all levels.

    • This reply was modified 8 years, 4 months ago by bcworkz.
    • This reply was modified 8 years, 4 months ago by bcworkz.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How do I get specific images from my post? (i.e. get image 1, 2, or 3)’ is closed to new replies.