最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - automatically set random featured image by category in wordpress on post

programmeradmin1浏览0评论

Basically I want to automatically set an image randomly from a selection of about 3 or 4 images in my Media Library when someone posts a blog from a particular category. I've got this so far but it only sets one image for that category.

function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);

if (!$featured_image_exists)  {
$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);
}}
else if ( in_category('49') ) {
set_post_thumbnail($post->ID, '2200');
    wp_reset_postdata();
}                      
                           }

      }
add_action('the_post', 'default_category_featured_image');

Is it possible to have more than one feature image randomly for each category?

Basically I want to automatically set an image randomly from a selection of about 3 or 4 images in my Media Library when someone posts a blog from a particular category. I've got this so far but it only sets one image for that category.

function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);

if (!$featured_image_exists)  {
$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);
}}
else if ( in_category('49') ) {
set_post_thumbnail($post->ID, '2200');
    wp_reset_postdata();
}                      
                           }

      }
add_action('the_post', 'default_category_featured_image');

Is it possible to have more than one feature image randomly for each category?

Share Improve this question edited Feb 20, 2020 at 13:32 Andrea Somovigo 1,6251 gold badge10 silver badges14 bronze badges asked Feb 20, 2020 at 6:10 Stefan GassnerStefan Gassner 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should prepare an array of images ids for each category and pick one of them randomly. I say "one of them" since the featured image is always 1 if I understood well your question.. which is somehow ambiguous when you say set an image and later to have more than one feature image So, editing part of your code and assuming that the rest is working:

//...
else if ( in_category('49') ) {
  // array of  ids of images for this category
  $arrImages = array(2200,2201,2202);

  // get random index from array $arrImages
  $randIndex = array_rand($arrImages);

  // output the value for the random index
  set_post_thumbnail($post->ID, $arrImages[$randIndex]);
  //wp_reset_postdata();

}
else if ( in_category('50') ) { // for example.. and so on
  $arrImages = array(2203,2204,2205);
  $randIndex = array_rand($arrImages);
  set_post_thumbnail($post->ID, $arrImages[$randIndex]);
}

wp_reset_postdata(); is not actually needed since you're not altering the main query see wp_reset_postdata()

发布评论

评论列表(0)

  1. 暂无评论