• Hi all,

    I have a little WordPress issue and your help would be amazing.

    Basically I have two custom post types one called Genres and the other called Testimonials. I am listing all the genres on the web page. This works well.

    However, I want to call all Testimonials relating to the certain genre.

    The testimonials has a custom taxonomy called genrescategory.
    So each time the admin add a testimonial they assign this is a genre.

    What I want to happen is within the loop calling all genres in the genres custom post type to call all the testimonials assigned to that genere name e.g. animation

    Here is the loop to get all genres:-

    <?php
      $args = array(
    	  'post_type' 		 => 'genres',
    	  'posts_per_page'    => -1
      );
      $posts=get_posts($args);
    
      if ($posts)
      {
    	  foreach($posts as $post)
    	  {
    		  setup_postdata($post);
    ?>
    <div id="<?php the_field('ancher_name'); ?>" class="genres-container">
      <h2 class="icon">
    	  <img src="<?php the_field('genres_what_we_do_page_icon'); ?>" width="66" height="64" alt="Animation">
    	  <?php the_title(); ?>
      </h2>
      <p><?php the_content(); ?></p>
    
      <div class="whowetalkto">
    	  <h3>Who we talk to...</h3>
    	  <?php if(get_field('who_we_talk_to')):  ?>
    		  <?php while(has_sub_field('who_we_talk_to')): ?>
    	  <div class="circular">
    		  <img src="<?php the_sub_field('profile_picture'); ?>" alt="<?php the_sub_field('full_name'); ?>">
    		  <figcaption><?php the_sub_field('full_name'); ?></figcaption>
    	  </div>
    	  <?php endwhile; ?>
    	  <?php endif; ?>
      </div>
    
      <!-- Add in testimonials for current genres in here -->
    
    </div>
    <?php
      }
    }
    ?>

Viewing 1 replies (of 1 total)
  • I do not have a way to test this, but it should be close to what you want. I assume that the field ‘ancher_name’ holds the name of the genre. If not, you will need to modify the line that sets $genre.

    Insert this code after the comment ‘Add in testimonials …’:

    <?php
       $genre = get_field('ancher_name');
       $args = array(
    	   'post_type' 		 => 'testimonials',
    	   'posts_per_page'    => -1,
    	   'genrescategory' => $genre
       );
       $testimonials = new WP_Query($args);
    
       if ($testimonials->have_posts()) {
    	   while ($testimonials->have_posts()) {
    	      $testimonials->the_post();
    		   // Show the testimonial here
    		   ?>
    		   <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    		   <?php
    		}
    	}
    	?>
Viewing 1 replies (of 1 total)
  • The topic ‘Call data from another post type inside a loop’ is closed to new replies.