Home › Forums › General Issues › $variable code not working
I have set up a custom taxonomy ‘media-tags’. The terms within ‘media-tags’ have an image added using ACF named ‘term_thumbnail’ (this works). I also am trying to add a class (moduleclass) to each list item with the code below – however it’s not working (any ideas why?). `$moduleclass = get_field('moduleclass');
$types[0] = 'media-tags';
foreach ($types as $type) {
$terms = get_terms($type, array('name_like' => “a”, 'order' => 'ASC', 'orderby' => 'name', 'exclude' => '26') );
if ($terms) {
foreach($terms as $term) {
$image = wp_get_attachment_image_src(get_field('term_thumbnail', 'media-tags_'.$term->term_id.''), 'thumbnail');
echo '<li class=”'.$moduleclass.'”>';
echo '<h2><a href=”/'.$term->slug.'”>'.$term->name.'</a></h2>';
echo '<img src=”'.$image[0].'”/></li>';
}
}
}`
Hi @surfsup74
Is the moduleclass
field a field on each term? Or is it a field somewhere else? If so, what is it saved to?
Moduleclass is a field that has been assigned to ‘Taxononmy’ = ‘media-tags’ using the location rules. The field then appears on the ‘Edit Media Tag’ page (the term). So, “yes, it’s field on each term”.
Hi @surfsup74
If the field is specific to the term, then you need to move the code WITHIN your terms loop and also use the $post_id parameter to target the taxonomy term.
You have done this correctly with the ‘term_thumbnail’ field, but for some reason, you have not followed this methodology with the ‘moduleclass’ field
Thanks Elliot.
I’ve moved the $moduleclass = get_field('moduleclass')
code inside the loop but am obviously doing something wrong as it’s still not working. Could you give an example of code that you’d expect to work please?
Hi @surfsup74
It isn’t working because you have forgotten to use the second parameter ($post_id).
You have done this correctly for term_thumbnail
so I don’t understand why you are having so much trouble for another field.
Is there something else I should know about your code? Did someone else write it?
Thanks
E
Thanks for your help Elliot. All sorted now.
<?php
// INCLUDE ONLY THE MOST IMPORTANT PHOTO TAGS IN THE LIST.
// 51 - Architecture
// 52 - Scapes
// 29 - UK
// 43 - Interiors
// 36 - Personal
// 38 - Commercial
// 30 - People
// 'orderby' => 'name', // sort by name or count (Alphabetical) or count (Most used)
// 'order' => 'ASC', // in ASCending or DESCending order
$types[0] = 'media-tags';
foreach ($types as $type) {
$terms = get_terms($type, array('name_like' => "a", 'order' => 'DESC', 'orderby' => 'count', 'include' => '51,52,29,43,36,38,30') );
if ($terms) {
foreach($terms as $term) {
$image = wp_get_attachment_image_src(get_field('term_thumbnail', 'media-tags_'.$term->term_id.''), 'thumbnail');
$genreDescription = get_field('genre_description', 'media-tags_'.$term->term_id.'');
$moduleClass = get_field('moduleclass', 'media-tags_'.$term->term_id.'');
echo '
<li class="'.$moduleClass.' block project">
<a href="/archive/photographs/'.$term->slug.'">';
echo '<img src="'.$image[0].'"/>
<div>
<div>
<h2>'.$term->name.'</h2>
'.$genreDescription.'
</div>
</div>
</a>
</li>';
}
}
}
?>
The topic ‘$variable code not working’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.