Support

Account

Home Forums Add-ons Repeater Field Repeater Field with Taxonomy Field

Solved

Repeater Field with Taxonomy Field

  • Hello,

    I have to build a calendar page and I use the Repeater Field, this need contain some info like: Title, date, where and when. this par of the while work well, I can see easy all the il for every row I make in the backend.
    But.
    In the second part I need show last 2 news for the specific event. In my posts I have mark it with different category name. Inside the Repeater Field I have added a Taxonomy Field. How I can integrate the Taxonomy Field inside the while of the Repeater Field?

    I know to have wrong using this array 'category_name' => $posts->name, but how I can set up it?
    The for foreach work, but I see the post from all category.

    Some one can help me. Thank you!

    <div class="calendar">
      <div class="all-events">
        <?php if( have_rows('events_list') ): ?>
               <ul class="slides">
              <?php while( have_rows('events_list') ): the_row();
                // vars
                $title = get_sub_field('nome_evento');
                $where = get_sub_field('luogo');
                $when = get_sub_field('data_e_ora');
                $month = get_sub_field('mese');
                ?>
                <li class="event-s">
                  <div class="info-event">
                    <p><?php echo $month; ?></p>
                    <h4 class="nome"><?php echo $title; ?></h4>
                    <p class="dove"><?php echo $where; ?></p>
                    <p class="quando"><?php echo $when; ?></p>
                  </div>
                  <div class="news-event">
                    <div class="loop-news">
                        <?php
                            $posts = get_field('latest_news');
                            $posts = get_posts(array(
                              'posts_per_page'  => 2,
                              'post_type'     => 'post',
                              'category_name' => $posts->name,
                            ));
                            if( $posts ): ?>
                              <ul>
                              <?php foreach( $posts as $post ):
                                setup_postdata( $post );
                                ?>
                                <li>
                                  <a href="<?php the_permalink(); ?>">
                                    <span class="img-post"><?php echo get_the_post_thumbnail( $p->ID ); ?></span><br>
                                    <span class="title-article"><?php the_title(); ?></span>
                                    <span class="excerpt"><?php echo get_the_excerpt( $p->ID ); ?></span>
                                  </a>
                                </li>
                              <?php endforeach; ?>
                              </ul>
                              <?php wp_reset_postdata(); ?>
                            <?php endif; ?>
                      </div>
                  </div>
                </li>
              <?php endwhile; ?>
              </ul>
            <?php endif; ?>
      </div>
    </div>
  • Assuming your field ‘latest_news’ is taxonomy field with single selection and returning the term object.

    the ‘category_name’ is actually not name, it’s the slug. (i know… it’s confusing :/)
    https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    So, you query should be:

    
    $category = get_field('latest_news'); // change your variable name so it doesn't confuse yourself
    
    $posts = get_posts(array(
        'posts_per_page'  => 2,
        'post_type'     => 'post',
        'category_name' => $category->slug, // this is using slug, not name
        'cat' => $category->term_id // or you can do this instead
    ));
    

    Cheers

  • Hi Gummi!
    Thank you! You have absolutely right!!!
    Now work well.

    I have set the variable inside the while, using get_sub_field because it’s inside of one Repeater Field. $category = get_sub_field('latest_news').
    Also I have set the Taxonomy Filed inside the post.
    And the 'category_name' => $category->slug, work perfectly!

    Thank you so much!

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

The topic ‘Repeater Field with Taxonomy Field’ is closed to new replies.