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


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.

    $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) ): ?>
        <!--Section Title-->
            <?php the_field( 'related_games_title', 'term_'. $cat_id ); ?>
        <!--Repeat field-->
        <?php if( have_rows('related_game', 'term_'. $cat_id) ): while ( have_rows('related_game', 'term_'. $cat_id) ) : the_row(); ?>
            <?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">
                <?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 class="smallgreencta">
                        <?php the_sub_field('cta_text'); ?>
            <?php endif; ?>
        <?php endwhile; else :
        // no rows found
        endif; ?>
    <?php endif;?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.