Support

Account

Home Forums General Issues images as choices in select field

Solved

images as choices in select field

  • Hi!

    I would like my users to be able to select a brand in one of my field groups.
    When they select the brand by textual name, the output should result in an image, the logo of that brand instead of only the textual brand name.

    Is this achievable?

    Thanks!

  • The easiest way to achieve this would be to have an if statement in code that retrieves the image from a url based on the selected option. ie:

    <?php
    $image_url = "";
    $option = get_field("selected_option");
    
    if($option=="option1"){
      $image_url = "image1.jpg";
    }else if($option=="option2"){
      $image_url = "image2.jpg";
    }
    
    ?>
    <img src="<?php echo $image_url; ?>" alt="Image">
  • Thank you for your help!

    I was thinking of an if statement as well, but know that the selection field might grow to a lot of options, like 50+ easily. This means I will get a huge if statement, wouldn’t that be a problem?

    Ah wait, I just found an option. If I name all my pictures “logo-brandname.jpeg” I can make a map in my /images folder with brand logos. then I can just upload the pictures to that map, e.g. “logo-coke.jpeg”, “logo-apple.jpeg”, “logo-sony.jpeg”
    If I then make sure that the select field options match the brand name in the filename I can just use <img src="/images/brand/logo-<?php the_field('reviewBrand'); ?>.jpeg"> right? that would be very convenient!
    Thanks for helping me on this!

  • Yep, thats another way to do it, just have to keep your naming convention consistent.

  • Although this is marked as solved, I’d like to suggest an alternative approach. I always like to create a workflow that could be maintained by a novice user even if it means a bit of extra programming.

    So why not create a CPT called Brands which contains just the title and image? I’d position its admin menu low down to avoid visual clutter or even make it a sub-menu, then create an ACF field as a relational post-object that would be selectable by title but displayable as an image without having to mess around with FTP uploads and careful file naming.

  • Thanks! setting this up right now but I can’t seem to display the post object as the “manufacturer_logo” fields of the manufacturers post type…
    How do I make the post object relational field output as an image?
    Thanks!

  • Set up the Post Object field to return Post ID, then use that ID to get the field from your CPT.

    
    // this would be somewhere in your post loop
    $logo_post_id = get_field('post_object_field');
    if ($logo_post_id) {
      get_field('logo_image_field', $logo_post_id)
      // what you do next depends on if your image field returns 
      // Image Array, Image URL or Image ID
    }
    

    I usually like returning Image IDs and then using wp_get_attachment_image_src() to get the image size I need rather then sorting through an array or using the URL returned by ACF so that I can determine the size used in the code.

  • I do not get it, Im sorry.
    Here my situation clearly explained:

    I have a custom post type called ‘manufacturers’ this custom post type has a custom field called ‘manufacturer_logo’, this is an image field that returns the image url.

    Now in my other custom post type, called ‘reviews’ I created a custom field called ‘manufacturer’ this is a ‘post object’ field type that has post type set at ‘manufacturers’

    When I add a review and select a manufacturer, I need the php code that pulls the ‘manufacturer_logo’ field from the selected post in the ‘manufacturers’ field and echoes the image url.

    for example, I add a review and select ‘Apple’ as the manufacturer, Apple is a custom post and contains a image in the ‘manufacturer_logo’ field, I need the url of that image to be echoed on the review page.

    You get it? Thanks for trying to help! really appreciate it!

  • On your review page, set the post object field for the manufacturer to return the id of the manufacturer post.

    
    /* 
       this would be somewhere in your post loop on review page
       set the manufacturer field to return id of manufacturer post object
    */
    $mf_post_id = get_field('manufacturer');
    if ($mf_post_id) {
      /*
         $mf_post_id should hold the manufacturer post id where the image
         image field is located. Now get the image field from that
         manufacturer post
      */
      $image = get_field('manufacturer_logo', $mf_post_id)
      /*
         $image should contain the value returned for the
         logo image field on the manufacturer post
      */
    }
    

    Hope that explains it better.

  • Thank you! However, I see no option to set the ‘post object’ field type to return a post ID value… there is no option at all for the return value

    Now when I put this in my review template inside the loop:

    <?php $mf_post_id = get_field('manufacturer');
       if ($mf_post_id) {
       $image = get_field('manufacturer_logo', $mf_post_id)
       }
    ?>

    the page does not load anymore, meaning there’s an error.
    What should I do?

  • Hmm… your using ACF4… ACF5 has the option. I guess that needs to be thr first follow up question now (what version are you using 😛 )

    First thing you should do is to turn on debugging while you’re doing development on a site. http://codex.wordpress.org/Debugging_in_WordPress. That way you’ll get hints as to why a page crashes and goes blank.

    Second, ACF4 compatible code.

    
    <?php $mf_post = get_field('manufacturer'); // changed variable name
       if ($mf_post) {
       $image = get_field('manufacturer_logo', $mf_post->ID); // changed to use object
       }
    ?>
    
  • @hube2
    I created a custom post 1, added ACF fields (image name and image to return image URL).
    Then on the ACF fields groups of another custom post 2, I selected Post Object field to return ID then…. On the Add New post of the Custom Post 2, i get a drop-down (select field) of the ACF fields meant for custom Post 1.

    On the public page, I used the exact code as above and I get the image correctly.

    $iconImage = get_field('icon_image', $section['icon_selector']);
    ---
    <img src="<?php echo $iconImage; ?>" alt="<?php get_field('icon_name', $section['icon_selector']); ?>">

    My challenge is the Drop Down/Select on the custom post 2 to select the image. It only shows the title of the record saved in custom post 1. How can I make this select field show image also?

    `

  • @jkkenzie I do not know if an image will work in a post object field.

    You should start here https://www.advancedcustomfields.com/resources/acf-fields-post_object-result/

    Post object fields do use select2 so I’m guessing that you could add the image, but I’m no sure.

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘images as choices in select field’ is closed to new replies.