Home > Software engineering >  Trying to fetch a posts sub category in Wordpress theme
Trying to fetch a posts sub category in Wordpress theme

Time:02-15

On my wordpress site, I have a section only displaying a lidt of post excerpts from within a specific parent category. Each post has a single sub-category which I'd like to also display alongside the title and excerpt.

At the moment I'm able to get and siaply the required posts but each post is displaying every subcategory, even ones not assigned to it. How can I modify this to show only assigned subcategories?

<div >
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div >    
        <p><span>
        <?php
            $args2 = array('child_of' => 1);
            $categories = get_categories( $args2 );
            foreach($categories as $category) { 
                $the_sub = $category->name;
                echo $the_sub;
            } ?>
         </span></p>   
         <h3><?php the_title(); ?></h3>  
         <p><?php the_excerpt(__('(more…)')); ?></p>
         <a href="<?php the_permalink() ?>">read more</a>
     </div>
     <?php endforeach; 
     wp_reset_postdata();?>
</div>

EDIT:

With Growdzens help I ended up with this that worked.

<div >
            <?php
              $args = array( 'posts_per_page' => 8, 'category' => 1 );
              $myposts = get_posts( $args );
              foreach ( $myposts as $post ) : setup_postdata( $post );
            ?> 
            <div >    
              <p>
                <span>
                  <?php
                    $categories = get_the_category( $post->ID);
                    foreach($categories as $category) { 
                      $the_sub = $category->name;
                      $cat1 = 4;
                      $cat2 = $category->cat_ID;
                      if( $cat2 != 1 ){
                        echo $the_sub;
                      }
                    }
                  ?>
                </span>
              </p>   
              <h3><?php the_title(); ?></h3>  
              <p><?php the_excerpt(__('(more…)')); ?></p>
              <a href="<?php the_permalink() ?>">read more</a>
            </div>
            <?php endforeach; 
            wp_reset_postdata();?>
          </div>

CodePudding user response:

Wordpress has a function for getting all categories of a post on which you can read more here: get_the_category()

This function can be used in the loop like the example below to get the desired results.

<div >
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div >    
        <p><span>
        <?php
            $categories = get_the_category( $post->ID);
            foreach($categories as $category) { 
                $the_sub = $category->name;
                echo $the_sub;
            } ?>
         </span></p>   
         <h3><?php the_title(); ?></h3>  
         <p><?php the_excerpt(__('(more…)')); ?></p>
         <a href="<?php the_permalink() ?>">read more</a>
     </div>
     <?php endforeach; 
     wp_reset_postdata();?>
</div>

Update: To check if a category is a child of a specific other category you can use the Wordpress function cat_is_ancestor_of()

<div >
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div >    
        <p><span>
        <?php
            $categories = get_the_category( $post->ID);
            $parent_id = 4; //change this ID to the parent ID you want to show the subcategories for
            foreach($categories as $category) { 
                $the_sub = $category->name;
                if(cat_is_ancestor_of( $parent_id, $category->cat_ID )){
                   echo $the_sub;
                }
            } ?>
         </span></p>   
         <h3><?php the_title(); ?></h3>  
         <p><?php the_excerpt(__('(more…)')); ?></p>
         <a href="<?php the_permalink() ?>">read more</a>
     </div>
     <?php endforeach; 
     wp_reset_postdata();?>
</div>
  • Related