Support

Account

Home Forums Add-ons Repeater Field Query custom taxonomy name and link inside Repeater

Solved

Query custom taxonomy name and link inside Repeater

  • Hi, I’m building a photographer’s site where the homepage will show the events’ categories (events is a custom post type and their categories are custom taxonomies) as hero images, one bellow the other.

    I decided to use the Repeater to let the client choose which categories will display, so inside the repeater I put a Taxonomy field, but when I call the taxonomy, nothing happens. I searched the entire forum and googled for someone with a similar issue and I couldn’t find it.

    Any ideas?

  • It’s not really clear from your post what you’re trying to do.

    You have a repeater field and in this field there is a taxonomy sub field.

    Are you trying to loop through the repeater and show a link to each term?

    Or are you somehow trying to query posts to find the ones that have a term selected in one of the rows of the repeater?

  • Thanks for your reply, John!

    Re-reading the post I may agree with you, hehe.

    Let’s clear the things.

    I have a repeater field called home_sections and inside it there’s a taxonomy field called event_category and a image field called category_image.

    What I need is to show the name and the link of these selected categories on the site (list items, div, whatever).

  • So it’s the first one 🙂

    
    <?php 
      // loop through the repeater
      if (have_rows('home_sections')) {
        while (have_rows('home_sections')) {
          the_row();
          // see: http://www.advancedcustomfields.com/resources/taxonomy/
          $term = get_sub_field('event_category');
          // I'm assuming that the taxonomy is "event_category"
          // change next line if that's wrong
          $taxonomy = 'event_category';
          $term_link = get_term_link($term, $taxonomy);
    
          // assuming you're returning image array
          //see: http://www.advancedcustomfields.com/resources/image/
          $image = get_sub_field('category_image');
          ?>
            <a href="<?php 
              echo $term_link; ?>><img src="<?php 
              $image['url']; ?>" alt="<?php 
              echo $image['alt']; ?>" /></a>
          <?php
        } // end while have rows
      } // end have rows
    
    ?>
    
  • Hi John.

    I used the code you wrote, but it returns the following error right after the href, or where <?php echo $term_link; ?> is

    Catchable fatal error: Object of class WP_Error could not be converted to string

  • The problem is that the taxonomy is probably incorrect, or the taxonomy field is not returning the right value. Check to make sure you have the right taxonomy name. Also check to make sure you’re actually getting term from the taxonomy field

    
    $term = get_sub_field('event_category');
    echo $term;
    
  • I reviewed the functions.php, rewrote some parts and now it works, but the <?php $image[‘url’]; ?> returns nothing and if I change to <?php echo $image[‘url’]; ?> it returns the url with spaces instead of slashes. On the image field the result is set to image array.

  • Solved, it was missing a ” after <?php echo $term_link; ?> 😉

    Thank you so much John!

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Query custom taxonomy name and link inside Repeater’ is closed to new replies.