WordPress.org

Forums

[resolved] Attaching an image and using elsewhere on the page (9 posts)

  1. ransom1337
    Member
    Posted 4 years ago #

    Hello,

    How would I go about attaching an image to a post without inserting it, and then what PHP would I use to display it in a separate div in index.php?

    Thanks!

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Uploading the image whilst editing a post will automatically attach the image to that post.

    what PHP would I use to display it in a separate div in index.php

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

  3. ransom1337
    Member
    Posted 4 years ago #

    Seeing as I've uploaded only one image to this post, and in the Media section it says it is attached to it, can I assume that the ID for this image is 1?

    Because I noticed on the codex for wp_get_attachment_image:

    Returns an HTML image element representing an attachment file, if there is any, otherwise an empty string.

    This is exactly what's happening for me. Why can't it find the image?

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    can I assume that the ID for this image is 1

    No. Like post ids, image ids are sequential. So the image will have the next id in the sequence - irrespective of what post it's attached to. If you place your cursor over any attachment file in the Media Library list, you should see something like

    http://domain_name/wp-admin/media.php?attachment_id=xxx&action=edit

    in the lower left of your web browser. attachment_id=xxx gives you the id for that specific attachment.

  5. ransom1337
    Member
    Posted 4 years ago #

    Okay, thanks. I'll be putting this in The Loop, meaning I want an image to go with each post on the front page, where all the recent posts are shown. What code would I use to dynamically put the corresponding image with the correct post?

    For example, is there anything in the get_child function that lets me obtain the attached image to a post?

  6. ransom1337
    Member
    Posted 4 years ago #

    Got it... it was:

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, false);
    	}
    }
    
    ?>
  7. esmi
    Forum Moderator
    Posted 4 years ago #

  8. ransom1337
    Member
    Posted 4 years ago #

    Heh thanks, that's the one I posted!

    Would you happen to know how to remove the hyperlink from the image completely? I don't want it linking to the attachment page, which it seems to do automatically, and I can't find where it's coming from.

  9. ransom1337
    Member
    Posted 4 years ago #

    Got this too. It's:

    <?php
    
    $args = array(
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'post_status' => null,
    		'post_parent' => $post->ID
    		);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo wp_get_attachment_image($attachment->ID, false);
    				}
    			}
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.