0

I'm trying to set multiple images (image A,B and C) from one folder (folder X) at random into the database as a default featured image when there's none one set.

So on the main page I have all posts with different featured images from folder X. After clicking on read later the featured image needs to be the same as the main page.

So, when posting something it needs to get one of the images from folder X as featured image if none was added..

Does anyone know how to get this working? I'm a newb PHP person myself (don't shoot the designer)..

1

Im not sure about the random part. But to set a back up featured image if none is set just use

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?> 

Or something like this without major template edits

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
  • Hi Noob Theory, I thought about the set a back up featured image but the problem is it's just one default image. I wonder if someone can add the random stuff to the second major template edit thing :-). Will try to add some things there. – rwzdoorn Apr 17 '13 at 11:16
0

Thanks to @Noob Theory for helping me out on the first part.

Solution: Upload 'default headers' to 'media' and check their ID's. Use array_rand to randomize the ID's et voila, your done.

<?
function autoset_featured() {
$media_array = array(
'1611',
'1612',
'1613',
);
$media = $media_array[array_rand($media_array)];
      global $post;
      $already_has_thumb = has_post_thumbnail($post->ID);
          if (!$already_has_thumb)  {
          $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                      if ($attached_image) {
                            foreach ($attached_image as $attachment_id => $attachment) {
                            set_post_thumbnail($post->ID, $attachment_id);
                            }
                       } else {
                            set_post_thumbnail($post->ID, $media);
                       }
                    }
  }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
?>
  • 2
    If Noob Theory's answer was not complete, it is very legitimate that you added a solution to your own problem yourself. But if it did help you significantly, you might want to consider upvoting his/her answer. – Johannes Pille Apr 19 '13 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.