wordpress Get Subcategories List custom PHP pages

wordpress Get Subcategories List custom PHP pages

In this Post We Will Explain About is wordpress Get Subcategories List custom PHP pages With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to List only child categories a post is in, of a specific parent categoryExample

In this post we will show you Best way to implement Putting Categories Into a Foreach Loop And manipulating, hear for How to Display Only Child Category in your WordPress Post Loopwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read This πŸ‘‰   wordpress Get All Parent Categories from Child Categories

To display the only simple sub or Child Category list in the post loop it is used to (mostly PHP custom single.php), all we have to do is replace the following code:

Example 1 : get subcategories of a category in wordpress

<?php the_category(', '); ?>

//using foreach loop to get all category list
foreach((get_the_category()) as $sub_category) {
	if (cat_is_ancestor_of(10, $sub_category)) {
	echo '<a href="'.get_category_link($sub_category->cat_ID).'">';
	 echo $sub_category->cat_name . '</a>';

	Angularjs 1
	Vuejs 2
	Laravel 1
	Magento 2
	Bootsrep 1
	Matrialize 2

Example 2 : display subcategories on category pages in wordpress

$main_parents = get_terms( 'category', array( 'parent' => 0 ) );
$list_catgory = get_the_terms( $post->ID, 'category' );
foreach( $main_parents as $get_parent ):
echo '<a href="' . get_term_link( $get_parent ) . '">' . $get_parent->name . '</a>: ';
$data_links = array();
foreach( $list_catgory as $get_cat ):
    if( $get_parent->term_id == $get_cat->parent ):
        $data_links[] = '<a href="' . get_term_link( $get_cat ) . '">' . $get_cat->name .      '</a>';
echo join( ', ', $data_links );

Example 3 : wordpress get child categories of current category

$all_category_list = array();
 $get_allCat = get_categories(); // Get the list of Categories
    foreach ($get_allCat as $main_catobj) {
        $term_id = $main_catobj->term_id;
        $posts_cat = get_posts(array('category'=>$term_id));
        $authors_arr = array();
        foreach ($posts_cat as $post_obj) {
            $author_id = $post_obj->post_author;
			$get_cat[$author_id][] = strtolower($main_catobj->name);
			$all_category_list[] = strtolower($main_catobj->name);
            if (!function_exists('get_userdata')) {
                include '<your WP folder>/wp-includes/pluggable.php';

            $main_user_obj = get_userdata($author_id);
            if (!in_array($main_user_obj->user_login, $authors_arr)) {
                $authors_arr[] = $main_user_obj->user_login; 
       echo implode(', ', $authors_arr) . '<br>';

$all_category_list = array_unique($all_category_list);
echo implode(', ', $all_category_list) . '<br>';


Also Read This πŸ‘‰   php format phone number with dashes

I hope you have Got What is List only child categories a post is in, of a specific parent category And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.