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;?>
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 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.