{"id":237,"date":"2020-08-03T14:11:56","date_gmt":"2020-08-03T14:11:56","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-show-a-list-of-child-categories-on-parent-a-category-page\/"},"modified":"2024-11-04T04:01:11","modified_gmt":"2024-11-04T04:01:11","slug":"how-can-i-show-a-list-of-child-categories-on-parent-a-category-page","status":"publish","type":"quick-tips","link":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-show-a-list-of-child-categories-on-parent-a-category-page\/","title":{"rendered":"How can I show a list of child categories on the category page for the current category?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">First, you must get the current viewing category\/term id and taxonomy name. We can get this calling get_queried_object(); function like below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$term = get_queried_object();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then you need to call the get_terms() function to get child categories like below: ( if you don&#8217;t want to show those categories which do not belong to any post, then set &#8216;hide_empty&#8217; => true )<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$child_categories = get_terms( $term->taxonomy, array(\n\t'parent'    => $term->term_id,\n\t'hide_empty' => false\n) );\n\nif ( !empty( $child_categories ) ) { \n\tforeach( $child_categories as $c_cat ) {\n\t\techo '&lt;li>&lt;a href=\"' . esc_url( get_term_link( $c_cat, $c_cat->taxonomy ) ) . '\">' . $c_cat->name . '&lt;\/a>&lt;\/li>';\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can use the above code to get child categories\/terms for posts or any custom post type(CTP) posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting child categories\/terms of a parent category on the category archive page is super simple. You need to use two WP functions which I have described here.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"tags":[],"topics":[43,44,45],"class_list":["post-237","quick-tips","type-quick-tips","status-publish","hentry","topics-woocommerce","topics-woocommerce-query","topics-wordpress"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"Getting child categories\/terms of a parent category on the category archive page is super simple. You need to use two WP functions which I have described here.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/quick-tips"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=237"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=237"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/topics?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}