2

Looking for any pointers really.

The functionality I'm after

Basically I'd like to have the functionality to assign up to 6 different images to one single post. All 6 images will be displayed as normal within single.php. On the homepage, for example, I'd then like one of those images to be randomly displayed on page load for that post.

A couple of questions

  1. Is this even possible?
  2. Is there a plugin that can manage this sort of thing?
  3. If I were to do it myself how should I go about creating this sort of functionality?
egr103
  • 3,858
  • 15
  • 68
  • 119

2 Answers2

4

Yes it is possible, and not all that difficult. You upload the images when creating the post.

Then in single.php you use get_children to get all images from the post.

assuming in the loop:

$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=$post->ID' );

and output them like so:

if ($images)
{
foreach ( $images as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id, 'full' );
    }
}

For your random image you could either use the same get_children as above but add &numberposts=1 to the args string.

or something like:

 function fetch_random_img($postid='') {
    global $wpdb;
    if (empty($postid))
    {
       //we are going for random post and random image
     $postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    }
    $imageid = $wpdb->get_var($wpdb->prepare("SELECT ID FROM wp_posts WHERE post_type='attachment' AND post_mime_type LIKE 'image/%' AND post_parent=$postid ORDER BY RAND() LIMIT 1"));
    if ($imageid) {
         echo wp_get_attachment_image( $imageid, 'full' );
    }
    else {
    return false;
    }

    }

This will give you only one random image, and it will be random, whereas get_children will pull out the same image each time unless you add order and orderby arguments, which will allow you to change which image comes out.

To echo the image within a div just call the function:

<div>
<?php fetch_random_img(); ?>
</div>
Liam Bailey
  • 5,879
  • 3
  • 34
  • 46
  • Did something go wrong in the line that reads ''post_type=attac`enter code here`hment&p'? It doesn't look right? Also when you output them using your code, what is that bit of code doing exactly? Is that to display all 6 images within single.php or is it to display a random post, for example on index.php? – egr103 May 22 '12 at 21:18
  • Yes, it happened when I clicked the curly brackets to put the code into a code block, I already took it out in one place. Anyway, sorted now. – Liam Bailey May 23 '12 at 05:59
  • So your code will pump out the 6 images within single.php? Do I just upload my images and insert them into the post as normal then? Forgive the questions, just not to confident with PHP. – egr103 May 23 '12 at 11:03
  • No. You upload the images as normal when adding the post, but you do not insert them into the post content. You simply hit save then publish. The code is what displays the images in single.php. OH, I forgot. About the randomly displaying on page load - on which page load - the single post page or another, and where/how would the image need to be displayed at random? – Liam Bailey May 23 '12 at 15:47
  • Well the random image functionality is really for other page/post templates and NOT single.php. On single.php I want to pump out all the images uploaded. On other templates I will want to call in just one of the however many uploaded images. – egr103 May 24 '12 at 11:37
  • RE: "You upload the images as normal when adding the post, but you do not insert them into the post content. You simply hit save then publish." Surely though when you do this, you are simply uploading to the generic Media folder where all post/page images are stored? How does the post know the images I upload are related to that specific post only? Again sorry for questions just finding it hard to understand how it will work. – egr103 May 24 '12 at 11:44
  • The parameter: post_parent=$post->ID in get_children tells it to pull all images attached to the post being output in single.php – Liam Bailey May 24 '12 at 11:57
  • Okay, that makes sense but what if the user wants to reuse an already uploaded image for a specific post? Would you have to upload that very same image again? What about the random functionality for other templates? – egr103 May 24 '12 at 12:19
  • Just want to ensure this method is flexible across the site thats all. I'd appreciate your thoughts :) – egr103 May 25 '12 at 15:23
  • All images uploaded are stored in the media library, so if you want to reuse an image, go into upload image - you have, from file, from url and media library, go into media library and search for the image you want to reuse. I will edit my answer to add the random image function – Liam Bailey May 26 '12 at 12:46
  • Thats great. Thanks. One LAST question & I will leave you to live the rest of your life ;) I've copied that random code into my functions.php file now how do I load it within a specific DIV so that a random image from a random post appears within it? Thanks again – egr103 May 28 '12 at 14:22
  • Sorry I never realised you wanted a random image from a random post, I thought you meant a random image from a given post. I will add an update -- please select answer as correct when we are done – Liam Bailey May 30 '12 at 07:57
  • somehow that trick does not work for me (using wp 3.4.2). I create a post, then upload an image and then press "Save all Changes". but then there are always shown all images that were ever uploaded. not just the ones that should belong to the post. – spierala Oct 01 '12 at 10:28
  • I made a mistake in the code of the function. Please try it now – Liam Bailey Oct 01 '12 at 20:07
  • it´s still strange. the get_children function does not really care of the value for post_parent. it always gets all images. this also happens with all the examples here: http://codex.wordpress.org/Function_Reference/get_children – spierala Oct 15 '12 at 11:28
  • ok like this it worked: $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent='.$post->ID ); – spierala Oct 17 '12 at 15:39
  • Yes, you are right, my bad. The variable wouldn't register inside single quotes so you need to use double quotes of concatenation as you have done. Sorry I never spotted that. – Liam Bailey Oct 17 '12 at 21:19
  • Nice solution! Is there an easy way to get an overview of attached medias? For myself it would be alright but I think it might be hard for non-IT-specialist to understand how it works and more important : get rid of already uploaded but unwanted images. – robbash Apr 11 '14 at 11:36
-1

For each post, add a custom field with a name and a value. You can have the name as ImageURL1 and the value can be the URL of the image. Add as many custom fields to the post as you want. Example:

enter image description here

Print it out in your single.php or any other file inside the loop using this code:

<?php $values = get_post_custom_values("ImageURL"); echo $values[0]; ?>

To load it on the homepage, you would query the post, and then get the custom field value for a specific name inside your index.php:

<?php query_posts('cat=10')  //your cat id here ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<a href="<?php $values = get_post_custom_values("LinkURL"); echo $values[0]; ?>" target="_blank"><img src="<?php $values = get_post_custom_values("ImageURL"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /></a>
<?php endwhile; ?><?php endif; ?>
<?php wp_reset_query(); ?>

You can randomize it or loop through the custom fields if you want.

anuragbh
  • 603
  • 1
  • 5
  • 11