Skip to main content
added 9 characters in body
Source Link
Andrea Somovigo
  • 1.6k
  • 1
  • 10
  • 14

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 salwaysalways 1 if I understood well your question.. which is somehow ambiguous whnwhen 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()

You should prepare an array of images ids for each category and pick one of them randomly. I say "one " since the featured image is salways 1 if I understood well your question which is somehow ambiguous whn 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()

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()

Source Link
Andrea Somovigo
  • 1.6k
  • 1
  • 10
  • 14

You should prepare an array of images ids for each category and pick one of them randomly. I say "one " since the featured image is salways 1 if I understood well your question which is somehow ambiguous whn 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()