Support » Fixing WordPress » Get Image URL/SRC

Viewing 12 replies - 1 through 12 (of 12 total)
  • esmi

    (@esmi)

    Forum Moderator

    @esmi The problem is that I don’t know how to get the ID dynamically.

    esmi

    (@esmi)

    Forum Moderator

    Can you grab the post id of the Post/Page that the image is attached to?

    I believe so? I’m not a developer so I’m not sure of how to go about that. Although, I’m certainly willing to learn.

    esmi

    (@esmi)

    Forum Moderator

    You could grab all of the image attachments for that Post/Page using something like:

    $args = array(
    	'post_parent' => $id, // this is your Post or Page id
    	'post_status' => 'inherit',
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    );
    $attachments = get_children( $args );?>

    Then it’s a case of deciding which of the images in the returned array you want (the array keys will be the images’ ids).

    http://codex.wordpress.org/Function_Reference/get_children

    @esmi Okay, great. So I got the actual ID of the main images using the following code.

    <?php
    		$args = array(
    			'numberposts' => 1,
    			'post_mime_type' => 'image',
    			'post_parent' => $post->ID,
    			'post_type' => 'attachment'
    			);
    
    		$get_children_array = get_children($args,ARRAY_A);  //returns Array ( [$image_ID]...
    		$rekeyed_array = array_values($get_children_array);
    		$child_image = $rekeyed_array[0];  
    
    		echo $child_image['ID'];   	//Show the $child_image ID.
    	?>

    Now the issue is how can I take that ID and get the source of the URL and echo it?

    Edit: Nevermind. I’m using a plugin called Attachments and it’s grabbing some of those as well and not the main ones inserted in the HTML Editor.

    esmi

    (@esmi)

    Forum Moderator

    Do you want the image’s src or the actual image itself?

    @esmi I edited the post above. Something isn’t working quite right.

    esmi

    (@esmi)

    Forum Moderator

    You might need to drop that plugin. 🙁

    @esmi All right but before I do that is there no way of grabbing the VERY FIRST image URL of each post?

    esmi

    (@esmi)

    Forum Moderator

    $args = array(
    	'numberposts' => 1,
    	'order'=> 'DESC',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID,
    	'post_type' => 'attachment'
    	);
    
    $get_children_array = get_children($args,ARRAY_A);  //returns Array ( [$image_ID]...
    $rekeyed_array = array_values($get_children_array);
    $child_image = $rekeyed_array[0];  
    
    print_r($child_image);  	//Show the contents of the $child_image array.
    echo $child_image['ID'];   	//Show the $child_image ID.

    @esmi It seems I have solved this using a function to grab the first image of a post and then echoing it where I need it while keeping the plugin.

    <?php
    	if( function_exists( 'attachments_get_attachments' ) )
    		{
    		$attachments = attachments_get_attachments();
    		$total_attachments = count( $attachments );
    	if( $total_attachments ) : ?>
    	<ul class="process"><span>Process:</span>
    	<a href="<?php echo catch_that_image() ?>"; ?>
    	<?php echo '1</a>'; ?>
    
    	<?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    			<li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+2; ?></a></li>
    	<?php endfor; ?>
    		</ul><br>
    	<?php endif; ?>
    	<?php } ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Get Image URL/SRC’ is closed to new replies.