I have a custom post type 'Holidays' and a custom field (created using Advanced Custom Fields) called 'activity_rating'. The activity_rating field is a select dropdown with values 1 to 5.
If the holiday has been rated as very easy (1) then I would like to display a certain image (1 star), if the holiday has been chosen to be easy (2) then I would like a different image to be displayed (2 stars) etc all the way to 5 (very difficult).
So far I have this from ACF's website but am struggling as to add/how edit
<?php
/*
* Conditional statement (Single Value)
*/
if(get_field('activity_rating') == "1")
{
//...
}
Any ideas very much appreciate!
I have a custom post type 'Holidays' and a custom field (created using Advanced Custom Fields) called 'activity_rating'. The activity_rating field is a select dropdown with values 1 to 5.
If the holiday has been rated as very easy (1) then I would like to display a certain image (1 star), if the holiday has been chosen to be easy (2) then I would like a different image to be displayed (2 stars) etc all the way to 5 (very difficult).
So far I have this from ACF's website but am struggling as to add/how edit
<?php
/*
* Conditional statement (Single Value)
*/
if(get_field('activity_rating') == "1")
{
//...
}
Any ideas very much appreciate!
you can try this:
if(get_field('activity_rating') == "1") {
echo 'path/to/image/onestar.jpg';
} elseif (get_field('activity_rating') == "2") {
echo 'path/to/image/twostar.jpg';
} elseif (get_field('activity_rating') == "3") {
echo 'path/to/image/threestar.jpg';
} elseif (get_field('activity_rating') == "4") {
echo 'path/to/image/fourstar.jpg';
} else (get_field('activity_rating') == "5") {
echo 'path/to/image/fivestar.jpg';
}
You can try this:
Upload all your images to your Media library.
Head over to your custom post type template, say single-holidays.php
.
Add this code to where you want to show the image
<?php
//Get the activity_rating field value from the current post
$activity_rating = get_field( 'activity_rating' );
//Create an array of images URL uploaded to your media library
$image_urls = array(
0 => '/url_to_default_image',
1 => '/url_to_image_1',
2 => '/url_to_image_2',
3 => '/url_to_image_3',
4 => '/url_to_image_4',
5 => '/url_to_image_5'
);
//Show the image correspond to the activity_rating field value if it's not empty
if ( ! empty ( $activity_rating ) && in_array( $activity_rating, range( 1, 5 ) ) ) {
echo '<img src="'. $image_urls[ $activity_rating ] .'" />';
} else {
echo '<img src="'. $image_urls[0] .'" />';
}
?>
Don't forget to change the URL of the images to correct ones inside the $image_urls
array.
That should do the trick!