Search code examples
phpwordpresswordpress-themingcustom-post-typetaxonomy

Wordpress Custom post type_show post counts


I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.

I changed the category to taxonomy.Now, i want to show more info under each category name,but, i cannot understand,where should i put my code in the loop. Specially the post counts under each categories.

 <?php
    /*
    Template Name: Home Page
    */
    get_header();
     global $redux_demo;

    ?>
    <div class="sroll"><div class="container">
    <marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
    </div></div>
    <div class="container">
     <div class="row">
      <div class="col-sm-9"> </br>
      <div class="content mCustomScrollbar" style="height: 690px;">

        <?php
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => false,
    ) );
    foreach($terms as $cat){
        $cata_name = $cat->name; 
        $term_id = $cat->term_id; 

    ?>
    <div class="col-sm-6 col-md-4 col-lg-3 p10">
    <div class="box">
         <?php 
    //echo '<h3>'.$catname[0]->cat_name.'</h3>';

    ?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
        <?php echo $cata_name; ?></a></h3> <?php

    $catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' ); 
    while($catqueryy->have_posts()) : $catqueryy->the_post();
    ?>

        <p class="post_title"><?php echo '<a  href="'.home_url('index.php/category/'.$cata_name).'">'.__(get_the_title(),'rockon').'</a>'; ?></p>
        <p class="post_cont"><?php echo get_the_excerpt(); ?></p>
        <?php
    endwhile; 
    ?>
    </div>

        </div>

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

      <div class="col-sm-3">
      <h1></h1>
       <?php get_sidebar(); ?>
      <h1></h1>
      </div>

     </div>
    </div>

    <?php
    get_footer();
    ?>

Solution

  • Custom taxonomy try:

    $the_query = new WP_Query( array(
        'post_type' => 'CUSTOM_POST_TYPE',
        'tax_query' => array(
            array(
                'taxonomy' => 'CUSTOM_TAXONOMY',
                'field' => 'id',
                'terms' => TERM_ID
            )
        )
    ) );
    $count = $the_query->found_posts;
    

    https://wordpress.org/support/topic/counting-posts-within-categories

    For Example:

    <?php
        /*
        Template Name: Home Page
        */
        get_header();
         global $redux_demo;
    
        ?>
        <div class="sroll"><div class="container">
        <marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
        </div></div>
        <div class="container">
         <div class="row">
          <div class="col-sm-9"> </br>
          <div class="content mCustomScrollbar" style="height: 690px;">
    
            <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
        ) );
        foreach($terms as $cat){
            $cata_name = $cat->name; 
            $term_id = $cat->term_id; 
    
        ?>
        <div class="col-sm-6 col-md-4 col-lg-3 p10">
        <div class="box">
             <?php 
        //echo '<h3>'.$catname[0]->cat_name.'</h3>';
    
        ?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
            <?php echo $cata_name; ?></a></h3> 
        <?php
    
        $catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' ); 
        $count = $catqueryy->found_posts;
        ?>
        <h3><?php echo "Post Count : ".$count; ?></h3> 
        <?php
        while($catqueryy->have_posts()) : $catqueryy->the_post();
        ?>
    
            <p class="post_title"><?php echo '<a  href="'.home_url('index.php/category/'.$cata_name).'">'.__(get_the_title(),'rockon').'</a>'; ?></p>
            <p class="post_cont"><?php echo get_the_excerpt(); ?></p>
            <?php
        endwhile; 
        ?>
        </div>
    
            </div>
    
        <?php } ?>
        </div></br>
          </div> 
    
          <div class="col-sm-3">
          <h1></h1>
           <?php get_sidebar(); ?>
          <h1></h1>
          </div>
    
         </div>
        </div>
    
        <?php
        get_footer();
        ?>