WordPress.org

Support

Support » Themes and Templates » [Resolved] Attaching an image and using elsewhere on the page

[Resolved] Attaching an image and using elsewhere on the page

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

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

    @esmi

    Forum Moderator

    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

    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?

    esmi

    @esmi

    Forum Moderator

    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.

    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?

    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);
    	}
    }
    
    ?>
    esmi

    @esmi

    Forum Moderator

    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.

    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);
    				}
    			}
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Attaching an image and using elsewhere on the page’ is closed to new replies.
Skip to toolbar