Forums

Attach Image To More Than One Post (10 posts)

  1. tjramage
    Member
    Posted 9 months ago #

    I have done tons of googling on this, and I can't seem to find any proper information on it. From what I have gathered, WordPress doesn't allow you to attach an already-attached image to another post. At the minute, I must re-upload the same image if I want to do this. That seems very illogical to me...

    I am currently working on a photography portfolio theme and have spent all day trying come up with a solution, but failing. I have found code snippets on here that allow an image to be re-attached to a new post, but that then removes the link from the old post. What I need is to be able to have the image attached to both posts at the same time.

    Does anyone know of a solution? Any help is appreciated.

    Thanks,
    - Tim.

  2. duck__boy
    Member
    Posted 9 months ago #

    The reason for this is that the the database entry for the image is stored in the wp_posts table, and the post_parent field is used to identify which post the the image is attaced to.

    It is indeed a little frustrating, and perhaps in the future WP will introduce a way to do it (perhaps store Post Parent as an array, so that an attachment can have more than one parent?).

    Failing that you can still insert the image in to multiple posts in the Edit Posts section, it's just that it won't be displayed as in 'the gallery' (all it will do is add a link to your post), or you can use the same image as the featurerd image on multiple posts.

    Thanks.

  3. tjramage
    Member
    Posted 9 months ago #

    Thanks for the explanation duck__boy! I'll just have to explain to my client that if they want their portfolio images in a blog post too, they'll have to re-upload them again.

    I do hope WordPress come up with a better way of attachment handling in the future...

  4. duck__boy
    Member
    Posted 9 months ago #

    Is there only 1 image per post? If so, Featured Image will do what you want...

  5. tjramage
    Member
    Posted 9 months ago #

    No, unfortunately not. It's for the gallery post-format. I'm using get_posts() to grab all image attachments and put them in a jQuery slideshow:

    <?php if ( has_post_format( 'gallery' ) ) {
    	$args = array(
    	    'post_type'      => 'attachment',
    	    'post_parent'    => get_the_ID(),
    	    'post_mime_type' => 'image',
    	    'post_status'    => null,
    	    'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
    	if ($attachments) { ?>
    		<div class="slides">
    
    <?php foreach ($attachments as $attachment) {
    	$url = wp_get_attachment_image_src ( $attachment->ID, "large");
    	$gallery_thumb = vt_resize( $attachment->ID, '', 524, '', false); ?>
    			<a href="<?php echo $url[0]; ?>">
    				<img src="<?php echo $gallery_thumb[url] ?>" width="<?php echo $gallery_thumb[width] ?>" height="<?php echo $gallery_thumb[height] ?>" />
    			</a>
    	<?php } ?>
    
    		</div>
    <?php }
    } ?>

    Unless you can think of a better way??

  6. duck__boy
    Member
    Posted 9 months ago #

    In fairness that's pretty much what I do (Mine is rather complicated and needs to be simplified though!), but I've never come across the issues of needing the same image on multiple posts due to the nature of the site I run.

  7. tjramage
    Member
    Posted 9 months ago #

    Yeah, that will usually be the case for me too. However, there may be an occasion where the user wants to use one of the images in their portfolio as well...

    It's not limiting me in any way, so I can live with it. It's just not very logical or efficient in my opinion. It also means that you're uploading the exact same image again, which is wasting bandwidth and server space. I just hope the WP devs can come up with a better system soon.

    Thanks for all your help mate, much appreciated!

    - Tim.

  8. duck__boy
    Member
    Posted 9 months ago #

    No problem, and I also hope that a more elegent solution is provided in the future.

  9. abutterfield
    Member
    Posted 9 months ago #

    So I just found a plugin that might help you. It says that you can grab images from the post itself (as in images you have inserted into a post for display). If you put all of the images you wanted in a particular gallery into a post or page, in theory, if this plugin does what it says it does, you can grab all of those images and then manipulate them into a gallery or whatever format you were planning on using.

    The plugin url is Get the Image

    Hope this helps you in your project

  10. tjramage
    Member
    Posted 9 months ago #

    Hmmm, good find! I shall take a look at it. Thanks abutterfield.

Reply

You must log in to post.

About this Topic