Support

Account

Home Forums General Issues $variable code not working

Solved

$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>';
    						}
    					}
    				}
    				?>
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘$variable code not working’ is closed to new replies.