Support

Account

Home Forums Add-ons Repeater Field Extract individual category name + link for repeater field

Solved

Extract individual category name + link for repeater field

  • Hello!

    I have 3 cards set up as a repeater. Each card contains an image, the category title, and a link to its specific category index page.

    In ACF, via taxonomy, my ‘category_name’ is set as a term object.

    My image for each card loads.

    I am struggling with correctly extracting the category name and category link.

    I have reviewed this forum for the past 3 days and tried numerous variations. Thanks in advance for help. Here’s my current code.

        <!-- start REPEATING THIS CONTENT - ICON/class card wrapper -->
        <?php if(have_rows('category_preview_offering')): ?>
    
        <div class="class-card-wrapper">
    
          <?php while(have_rows('category_preview_offering')): the_row();
            //Vars
            $image = get_sub_field('image');
            $term = get_sub_field('category_name');
            $taxonomy = 'category_name';
            $term_link = get_term_link($term, $taxonomy);
    
            if($term): ?>
    
            <div>
            <!-- start card one -->
            <!--  <a href="/index.php?cat=4"> -->
            <a href="">
                <div class="icon_individual_card grow">
    
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
    
                    <p class="icon_text_color blog_category_title"><?php echo $term->slug; ?>
    
                </div>
            </a>
          <?php endif; ?>
          </div>
            <!-- end card one -->
          <?php endwhile; ?>
    
          <?php wp_reset_postdata(); ?>
    </div>
    <?php endif; ?>
    <!-- end ICON/class card wrapper -->
    </section>
    
    <!-- end BLOG CATEGORIES 3 ICON/class cards -->
    
  • This $taxonomy = 'category_name'; appears to be the name of the field and it should be the name of the taxonomy. If you’re using the built in WP category taxonomy it should be $taxonomy = 'category';

  • Thank you! Your answer was a part of my solution. For anyone else who stumbles upon this thread and wants to do something similar, here’s my code that works in my project.

        <!-- start REPEATING THIS CONTENT - ICON/class card wrapper -->
        <?php if(have_rows('category_preview_offering')): ?>
    
        <div class="class-card-wrapper">
    
          <?php while(have_rows('category_preview_offering')): the_row();
            //Vars
            $image = get_sub_field('image');
            $term = get_sub_field('category_name');
            $taxonomy = 'category';
            $term_link = get_category_link($term[0]->term_id);
    
            if($term): ?>
    
            <div>
            <!-- start card one -->
            <!--  <a href="/index.php?cat=4"> -->
            <a href="<?php echo $term_link; ?>">
                <div class="icon_individual_card grow">
    
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
    
                    <p class="icon_text_color blog_category_title"><?php echo $term[0]->name; ?></p>
                </div>
            </a>
          <?php endif; ?>
          </div>
            <!-- end card one -->
          <?php endwhile; ?>
    </div>
    <?php endif; ?>
    <!-- end ICON/class card wrapper -->
    </section>
    
    <!-- end BLOG CATEGORIES 3 ICON/class cards -->
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Extract individual category name + link for repeater field’ is closed to new replies.