conditional content - How to display image on condition that a selection has been made

admin2025-01-07  3

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!

Share Improve this question edited Apr 29, 2015 at 10:11 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Apr 29, 2015 at 8:46 pjsandopjsando 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

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:

  1. Upload all your images to your Media library.

  2. Head over to your custom post type template, say single-holidays.php.

  3. 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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254440a205.html

最新回复(0)