WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get Image URL/SRC (13 posts)

  1. christopherburton
    Member
    Posted 2 years ago #

    How can I get the image url in text (http://doman.com/some/img.png) outside of the loop?

    Visual Example:
    How can I get the URL of the images I post in the backend but outside the loop?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. christopherburton
    Member
    Posted 2 years ago #

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

  4. esmi
    Forum Moderator
    Posted 2 years ago #

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

  5. christopherburton
    Member
    Posted 2 years ago #

    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.

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    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

  7. christopherburton
    Member
    Posted 2 years ago #

    @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.

  8. esmi
    Forum Moderator
    Posted 2 years ago #

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

  9. christopherburton
    Member
    Posted 2 years ago #

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

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    You might need to drop that plugin. :-(

  11. christopherburton
    Member
    Posted 2 years ago #

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

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    $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.
  13. christopherburton
    Member
    Posted 2 years ago #

    @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 } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic