I’ve actually stunned myself and somehow figured this out. See my messy code below.
Each category has an image assigned in ACF (prod_image). I created a group “Taxonomy is equal to Category”, field type “Taxonomy” with radio buttons outputting a term ID.
<?php
$the_cat = get_the_category();
$the_cat = get_the_category( $id );
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<!--Related Games-->
<?php if( get_field('show_related_games', 'term_'. $cat_id) ): ?>
<div>
<!--Section Title-->
<h3>
<?php the_field( 'related_games_title', 'term_'. $cat_id ); ?>
</h3>
<!--Repeat field-->
<?php if( have_rows('related_game', 'term_'. $cat_id) ): while ( have_rows('related_game', 'term_'. $cat_id) ) : the_row(); ?>
<!--Product-->
<div>
<?php $term_id = get_sub_field('related_link');
if( $term_id):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id );
$prod_image = get_field( 'prod_image', 'term_'. $term_id);?>
<a href="<?php echo $term_url; ?>" class="relatedprod" target="_blank">
<!--Packshot-->
<?php if ( $prod_image ) { ?>
<img src="<?php echo $prod_image['url']; ?>" alt="<?php echo $prod_image['alt']; ?>" />
<?php } ?>
<!--Product Info-->
<div class="prodinfo">
<div class="gamename">
<?php echo $term_name; ?>
</div>
<div class="smallgreencta">
<?php the_sub_field('cta_text'); ?>
</div>
</div>
</a>
<?php endif; ?>
</div>
<?php endwhile; else :
// no rows found
endif; ?>
</div>
<?php endif;?>
This solved my issue, many thanks!
I completely forgot to give an example of what I’m trying to achieve. So if the article is about a film for example, let’s say Casablanca, I want the sidebar to have generic information that I filled out in the category creation like an image of a DVD cover for Casablanca and I want that to be true of all articles I write about Casablanca. This code should grab that image right?
<?php $prodimage = get_field( 'prodimage' ); ?>
<?php if ( $prodimage ) { ?>
<img src="<?php echo $prodimage['url']; ?>" alt="<?php echo $prodimage['alt']; ?>" />
<?php } ?>
I don’t know how to only pull the prodimage for the category of the post like the following code that pulls in the worpdress category description for the post:
<?php $catID = get_the_category(); echo category_description( $catID[0] ); ?>
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.