WordPress.org

Ready to get started?Download WordPress

Forums

Multiple values field into a list of images with each having *value*.jpg (6 posts)

  1. adzay
    Member
    Posted 1 year ago #

    Hi People, I am using a "Select" field that allows me to select multiple values. I understand how to use the "Implode" (that was given in the documentation part of this site) to show them as a list separated by a comma. My goal is to have the values bring up a list of images that have the value as the file name.

    E.g

    From my multiple value lise select field I select : 'House' 'Dubstep' 'pop' 'Hip hop'

    <?php echo implode(', ', get_field('page_layout')); ?>

    Using the implode function I get in a comma list: 'House, Dubstep, pop, Hip hop'

    I want to use php to bring Icons representing those names instead from my style sheet directory.

    so it will be *'directory'/House.Jpg*,*'directory'/Dubstep.Jpg*,*'directory'/pop.Jpg*, *'directory'/Hip Hop.Jpg*

    I want it like this because there are more values to select so I want it to load different images depending on what I select in my custom field.

    I have tried to play around with Echos and arrays but I have no luck. . Please note that i will be using the <?php bloginfo('template_url'); ?> to get the style sheet.

    Help meh

  2. tomontoast
    Member
    Posted 1 year ago #

    get_field('page_layout') returns an array. We can include them into a piece of html by using

    foreach(get_field('page_layout') as $array_item){
        echo "directory/".$array_item.".jpg ";
    }

    Have a play with that!

  3. adzay
    Member
    Posted 1 year ago #

    Hi thanks for your help. Using what you gave me I made this

    <?php foreach(get_field('music_genre_1') as $array_item){
        echo '<?php echo get_template_directory_uri(); ?>/images/icons/'.$array_item.".jpg ";
    }
    		?>

    but the get_template_directory will not work . It just shows the following:

    /images/icons/Hip-Hop.jpg
    /images/icons/Funky.jpg
    /images/icons/RnB.jpg
    /images/icons/Bashment.jpg

    How do i make it search in my template directory using that script?

  4. tomontoast
    Member
    Posted 1 year ago #

    Your code is not quite valid try:

    <?php foreach(get_field('music_genre_1') as $array_item){
        echo get_template_directory_uri(). '/images/icons/' .$array_item.".jpg ";
    }
    		?>
  5. adzay
    Member
    Posted 1 year ago #

    ahh now its listing the correct full directory. thanks man. Now how can i make the echo a img. Is it like this?

    <?php foreach(get_field('music_genre_1') as $array_item){
        echo '<img src = "get_template_directory_uri(). '/images/icons/' .$array_item.".jpg ""/>';
    }
    		?>
  6. adzay
    Member
    Posted 1 year ago #

    ahh yes I managed to do this.

    Unfortunately its was a trail and error method that I used so I did not really learn from it its (2:20 am here) .. He is the code that worked for me though

    <?php foreach(get_field('music_genre_1') as $array_item){
       $image = get_template_directory_uri(). '/images/icons/' .$array_item.".png ";
    
       $imagesquare = '<img src='.$image.'/>';
    
    echo $imagesquare;
    }
    		?>

Topic Closed

This topic has been closed to new replies.

About this Topic