PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Trying to fetch a posts sub category in Wordpress theme

 March 04, 2022     php, wordpress     No comments   

Issue

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 class="blog-items">
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div class="blue">    
        <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 class="blog-items">
            <?php
              $args = array( 'posts_per_page' => 8, 'category' => 1 );
              $myposts = get_posts( $args );
              foreach ( $myposts as $post ) : setup_postdata( $post );
            ?> 
            <div class="blue">    
              <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>

Solution

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 class="blog-items">
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div class="blue">    
        <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 class="blog-items">
    <?php
        $args = array( 'posts_per_page' => 8, 'category' => 1 );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
    ?> 
    <div class="blue">    
        <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>


Answered By - Growdzen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing