Some difficulties with ACF taxonomy usage

Hi,

I have created a Custom Post Type with ACF as well as two taxonomies (category and type) for the same CPT.
I try to use TWIG for the following tasks:

  1. I want the tile background color for each post in the archive to match its custom post type category. I have three categories and want to have three different colors. I use the following code. When I utilize a text element, the color codes are displayed correctly. However, if you use this code on the background field it doesn’t work.

     {% if acf.post_field({"field":"category"}) == 36 %}
     global-color:lkgMQvt4yhqVR2Qq3o
     {% elseif acf.post_field({"field":"category"}) == 37 %}
     global-color:w9CI4DQGHT2WfP2gBf
     {% else %}
     global-color:tmjfa017L2LYJoWru8
     {% endif %} 
    

The scenario with Icon Element mirrors this closely. If I put the following code inside a Text Element I see the icons names as they were planed. But as soon as I put it inside the Icon Element in breakout mode it doesn’t show any icons.

{% if acf.post_field({"field":"type"}) == 20 %}
volleyball
{% elseif acf.post_field({"field":"type"}) == 21 %}
volleyball
{% elseif acf.post_field({"field":"type"}) == 22 %}
futbol
{% elseif acf.post_field({"field":"type"}) == 23 %}
microphone-lines
{% elseif acf.post_field({"field":"type"}) == 24 %}
eye
{% elseif acf.post_field({"field":"type"}) == 25 %}
chart-pie
{% elseif acf.post_field({"field":"type"}) == 26 %}
user-group-simple
{% elseif acf.post_field({"field":"type"}) == 27 %}
earth-europe
{% elseif acf.post_field({"field":"type"}) == 35 %}
bags-shopping
{% else %}
circle-o
{% endif %}

What is wrong, please suggest.

Hello John,

Thank you for the inquiry.

Did you save these as Twig templates and insert them into the elements using the Template (Include) DC? We may need to inspect the ACF fields and the actual page where you’re testing this. Please provide the page URL and any related info in the secure note.

Best regards,
Ismael

Hi Ismael @Ismael,

Actually I tried both putting the code snippets directly into the respective fields as well doing that via Templates. For the icon I even tried to add an ‘icon’ parameter to link it to the actual icon input. For the event tile background you can see that only one redish color is applied. However, under the event time string we can see both Template’s outputs inside the text element for debug and the output different values. This what each TWIG snippet actually returns.

P.S. Currently I set both via Templates so that would be easier for you.

Thank you for the info.

The background template seems to work when placed in the Background Layers > Lower section, but for some reason the page doesn’t load on the frontend.

{% include 'cs-template:id-1' %}

Unfortunately, the way you are trying to retrieve the icons will not work. You may need to use actual images in the template or upload images to the Media > Library, replace the icon name with the ID of the images in the Twig template, then use the Image element. Place the Twig template in the Source field.

Example:

{% if acf.post_field({"field":"type"}) == 20 %} 123 {% elseif acf.post_field({"field":"type"}) == 21 %} 22 {% elseif acf.post_field({"field":"type"}) == 22 %} 45 {% elseif acf.post_field({"field":"type"}) == 23 %} 66 {% elseif acf.post_field({"field":"type"}) == 24 %} 77 {% elseif acf.post_field({"field":"type"}) == 25 %} 22 {% elseif acf.post_field({"field":"type"}) == 26 %} 422 {% elseif acf.post_field({"field":"type"}) == 27 %} 44 {% elseif acf.post_field({"field":"type"}) == 35 %} 23434 {% else %} 2334324 {% endif %}

The numbers there are arbitrary IDs of images in the Media > Library. Replace them with the actual ID of the images.

Hope this helps.

1 Like

Thank you Ismael,

I already tried similar thing at another site with real images and it worked. I hope that you will also add Font Awesome support for dynamic pull up at some point. Please make a request for that if it is not in your list yet. And thank you in advance for that.

Another question off this topic though.
As I have two different taxonomy units is that possible to create just one Archive template for each Custom Post Type taxonomy unit or I need to create template for each taxonomy item inside each unit? Currently this is the way I did that but maybe I do not understand how to link archive templates correctly so that one archive temp[late would output all the taxonomies of one type.

Thank you for the update.

You can use a single Archive Layout for multiple post types and taxonomies. Simply configure the Settings > Assignment > Conditions to cover all your use cases. If you want to use it for a specific taxonomy of a post type for example, select the Post Type Taxonomy and then add other conditions as needed, taking note of the OR and AND operators. Please check this documentation for more info: https://theme.co/docs/archive-templates

Let us know if you have more questions.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

And to finalise with my enquiry related to the dynamic backgrounds. Maybe this is the subject for your Devs further investigation. That snippet doesn’t work on other instances where you do not have Upper and Lower layers like in our case. For example if I try to use it to change the icons background to match my tile background color it doesn’t work there as well. Just put it to neutral white for now.

We’ll forward the thread to our channel for further investigation. Thank you for the info.

Here I mean that if we need to add each taxonomy in the conditions of the particular type that is not that good for the case when a user adds a new taxonomy and it is not added to conditions automatically. That would be nice if there would be an option to add all the taxonomies of the particular type besides one by one selection like it is done now.

Yes, you will have to update the Conditions when a new post type or taxonomy is created. Unfortunately, there is no way to automatically include them unless you created a condition set that covers all use cases. For example, using the condition “All Archives” in combination with the “Post Type” condition (make sure to use AND) should cover all archive pages including newly added taxonomies that belong to a specific post type.

Please feel free to open a new thread if you have more questions about the theme.

I will double check that but it seams to me that it didn’t output any events in this case when just the taxonomy type is used for the archive layout.