Support

Account

Home Forums ACF PRO Trying to display selected taxonomy terms and their images on home page.

Solved

Trying to display selected taxonomy terms and their images on home page.

  • Ok, so I want to display certain categories on the home page based on an advanced custom field group using multi selected checkboxes that are displayed when editing the home page.

    The workflow is that the user edits the home page and selects which taxonomy terms are displayed by checking the related check boxes.

    Now these taxonomy terms also have each one image attached to them so I need those images displayed too. So I have created another field group called “bd_category_images” and the user goes into the custom post type category and clicks edit, then uploads an image from there.

    The problem is this, the code I’m working with is generating the “Taxonomy Term” as per selected in advanced custom fields field group. I think I need to return the taxonomy ID and then have the Taxonomy ID used to display the images, then somehow have the Taxonomy ID used to display the Taxonomy term.

    Here is the code I am working with: (it doesn’t work)

    		<?php
    			$terms = get_field('home_page_categories');
    			if( $terms ): 
    		?>
    			<ul>
    				<?php foreach( $terms as $term ): ?>
    				<h2><a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a></h2>
    				<p><?php echo $term->description; ?></p>
    				<?php
    				$image = get_image('bd_category_images', $term);
    				echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" />';
    				?>
    		<?php endforeach; ?>
    			</ul>
    		<?php endif; ?>
    

    Basically the output is the first taxonomy term selected and that’s it, no image or anything, just the taxonomy term.

    If I comment out this code:

    				<?php
    				//$image = get_image('bd_category_images', $term);
    				//echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" />';

    Then it displays all taxonomy terms that I have selected when editing the home page, but doesn’t display the taxonomy term images…

    So what do I do? Thank you so much.

  • I figured this out. I totally misused the repeater field which caused an unnecessary headache.
    Anyway, this is the code I used. It’s a modified version of this code – https://support.advancedcustomfields.com/forums/topic/query-custom-taxonomy-name-and-link-inside-repeater/

    Hope this helps someone out.

    <?php
      // loop through the repeater
      if (have_rows('catimg_repeater')) {
        while (have_rows('catimg_repeater')) {
          the_row();
        $term = get_sub_field('choose_category');
        $term_link = get_term_link($term);
        $image = get_sub_field('choose_image');
    	$url = $image['url'];
    	$alt = $image['alt'];
          ?>
            <a href="<?php echo $term_link; ?>"><img src="<?php echo $url ?>" alt="<?php echo $alt ?>" /></a>
        <?php
    		} // end while have rows
    	  } 
    	?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.