Support

Account

Home Forums General Issues In category, select other categories and return their name, an image and link? Reply To: In category, select other categories and return their name, an image and link?

  • 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;?>