'How to display taxonomy post as its term?

I want to display taxonomy according to its term. But each and every terms are displaying. I want to display posts according to its terms. Here is my code.

 <?php 
 $terms = get_terms('location');
 foreach( $terms as $term ):
 ?>                          

 <?php                         
 $posts = get_posts(array(
 'post_type' => 'country',
 'taxonomy' => $term->taxonomy,
 'term' => $term->slug,                                  
 'nopaging' => true, // 
 'offset' => 0,
 ));
 foreach($posts as $post): 
 setup_postdata($post); 
 ?>        
 test    
 <?php endforeach; ?>
                                   
 <?php endforeach; ?>


Solution 1:[1]

Create 1 page in admin named Events like this screenshot : http://prntscr.com/flvw4j as per below code custom taxonomy post_type is "event" , taxonomy is "eventcategory" , so inside theme folder create event.php file and add below code inside it.

$temp = $wp_query; $wp_query= null;
            $Args = array(
                        'post_type' => 'event',
                        'showposts' => 6,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'eventcategory',
                                'field' => 'slug',
                            ),
                        ),
                        'paged'=>$paged,
                    );
           $wp_query = new WP_Query();
           $wp_query->query($Args);
           while ($wp_query->have_posts()) : $wp_query->the_post(); 
            get_template_part( 'content', 'event' );    
          endwhile;

then create content-event.php inside theme folder and here you can add below code (you can modify as per your needs and fields you want)

<li>          

<?php 
    if ( has_post_thumbnail() ) {
         $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
         $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );
?>
<div class="events-img">
  <?php 
                   if($image_attributes[1] > 325){
                              $width = 325;
                          }else{
                              $width = $image_attributes[1]; 
                          }

                          if($image_attributes[2] > 240){
                              $height = 240;
                          }else{
                              $height = $image_attributes[2]; 
                          }

     echo '<img src="'.$image_attributes[0].'" width="'.$width.'" height="'.$height.'"/>';

  ?>
</div>  
<?php } ?>

 <h3><a href="<?php the_permalink(); ?>">
      <?php  if (strlen($post->post_title) > 25) {
             echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...'; } else {
           the_title();
        }  ?></a></h3>

<?php echo get_the_time('F d', $post->ID); ?>
 <?php
        $terms = get_the_terms( $post->ID, 'eventcategory' );

        if ( $terms && ! is_wp_error( $terms ) ) : 

            $draught_links = array();

            foreach ( $terms as $term ) {
                $draught_links[] = $term->name;
            }

            $on_draught = join( " , ", $draught_links );
        ?>
         <!--<?php echo ", ".$on_draught; ?>!-->
         <?php endif; ?>

   <p><?php echo excerpt(150); ?></p>

   <?php
    //the_content();

    wp_link_pages( array(
        'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'solid_rock' ) . '</span>',
        'after'       => '</div>',
        'link_before' => '<span>',
        'link_after'  => '</span>',
        'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'solid_rock' ) . ' </span>%',
        'separator'   => '<span class="screen-reader-text">, </span>',
    ) );
    ?> 

then inside your themes functions.php file add below function

function excerpt($limit) {
    return wp_trim_words(get_the_excerpt(), $limit);
}
function solid_trim_excerpt($text)
{
    return '...';
}
add_filter('excerpt_more', 'solid_trim_excerpt');

Hope it helps you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1