Home › Forums › General Issues › In category, select other categories and return their name, an image and link?
Hi
Is there a way for my client to set up a category with a list of all the others so they can select similar categories.
On articles of a given category I would like to list those selected items in a sidebar, it would display the category name, an image assigned to that category in ACF and a link to the category.
I’ve been searching but I can’t find relevant documentation. If someone could point me in the right direction that would be superb.
I’m using pro and I’m teaching myself ACF and PHP as I go.
Thanks in advance.
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;?>
The topic ‘In category, select other categories and return their name, an image and link?’ 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 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.