• How i can show all sub category posts in each category page by block and its name(Subcategory name) if have subcategory and its posts?
    Example :let’s I have a main category Sports and its subcategory are Cricket,Ft Football and other . Please see the link image that i have mentions.

    Thanks
    Ala

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Joey

    (@leglesslizard)

    Hi,

    Have a look at get_terms() (https://developer.wordpress.org/reference/functions/get_terms/). This will allow you to retrieve all terms for a custom taxonomy (e.g. sports) and loop through the terms displaying any information you require.
    Within this loop you could then run a tax query (https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters) to retrieve all posts for the current term. For example:

    $sports = get_terms( array( 'taxonomy' => 'sport' );
    foreach( $sports as $sport ) {
      echo $sport->name;
      $args = array(
        'post_type' => 'post_type',
        'tax_query' => array(
          array(
            'taxonomy' => 'sport',	
    	'field'    => 'slug',
    	'terms'    => $sport->slug,
          ),
        ),
      );
      $query = new WP_Query( $args );
      if( $query->have_posts() ):
        while( $query->have_posts() ): $query->the_post();
           //Output information for post
        endwhile;
      endif;
      wp_reset_postdata();
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Subcategory post in category.php page’ is closed to new replies.