{"id":28,"date":"2017-04-20T07:20:14","date_gmt":"2017-04-20T07:20:14","guid":{"rendered":"http:\/\/webprow.com\/blog\/?p=28"},"modified":"2020-01-03T10:27:07","modified_gmt":"2020-01-03T10:27:07","slug":"child-pages-for-a-parent-page","status":"publish","type":"post","link":"https:\/\/webprow.com\/blog\/child-pages-for-a-parent-page\/","title":{"rendered":"Display a List of Child Pages For a Parent Page in sidebar"},"content":{"rendered":"<p>In this tutorial to show How to get child pages of specific parent page in sidebar dynamically<\/p>\n<p>Ex : Products is a parent page and Product-1,product-2,etc are child page(sub-pages)<\/p>\n<p><strong>STEP 1 :<\/strong> To get child pages<\/p>\n<p>Add the below code in <strong>functions.php\u00a0<\/strong><\/p>\n<pre>function show_all_children($parent_id, $post_id, $current_level)\r\n{ \r\n$top_parents = array();\r\n$top_parents = get_post_ancestors($post_id);\r\n$top_parents[] = $post_id;\r\n\r\n$children = get_posts(\r\n array(\r\n 'post_type' =&gt; 'page'\r\n , 'posts_per_page' =&gt; -1\r\n , 'post_parent' =&gt; $parent_id\r\n , 'order_by' =&gt; 'title'\r\n , 'order' =&gt; 'ASC'\r\n));\r\n\r\nif (empty($children)) return;\r\n\r\necho '&lt;ul class=\" sub-pages children level-'.$current_level.'-children\"&gt;';\r\n\r\nforeach ($children as $child)\r\n{\r\nif(is_page($child-&gt;ID ))\r\n{\r\n $sctive=\"child-page-active\"; \/\/class of current(active) child page \r\n \r\necho '&lt;li class=\"p-4 mt-0 '.$sctive.'\"';\r\n}\r\nelse \r\n{ \r\necho '&lt;li class=\"p-4 mt-0\" ';\r\n}\r\n \r\necho '&gt;';\r\n\r\necho '&lt;span class=\"glyphicon glyphicon-triangle-right\" aria-hidden=\"true\"&gt;&lt;\/span&gt;\r\n&lt;a class=\"\" href=\"'.get_permalink($child-&gt;ID).'\"&gt;';\r\necho apply_filters('the_title', $child-&gt;post_title);\r\necho '&lt;\/a&gt;';\r\n\r\n\/\/ now call the same function for child of this child\r\n if ($child-&gt;ID &amp;&amp; (in_array($child-&gt;ID, $top_parents)))\r\n {\r\n show_all_children($child-&gt;ID, $post_id, $current_level+1);\r\n }\r\n\r\necho '&lt;\/li&gt;';\r\n}\r\n\r\necho '&lt;\/ul&gt;';\r\n}<\/pre>\n<p><strong>STEP 2 :<\/strong> To display the list of child pages<\/p>\n<p><strong>Note :<\/strong> In this i&#8217;ve displayed the child pages title as list ,you can add div, section,etc instead of &#8216;&lt;li&gt;&#8217; \u00a0tag.<\/p>\n<p>Add this code to where ever you want to display the lists.Add in sidebar &#8216;text widget&#8217; to get the list in sidebar.<\/p>\n<pre>&lt;h2&gt;\r\n&lt;?php \r\n$current = $post-&gt;ID;\r\n$parent = $post-&gt;post_parent;\r\n$grandparent_get = get_post($parent);\r\n$grandparent = $grandparent_get-&gt;post_parent;\r\n?&gt;\r\n\r\n&lt;?php \r\nif ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) \r\n{\r\necho get_the_title($grandparent); }else {echo get_the_title($parent); \r\n}\r\n?&gt;\r\n&lt;\/h2&gt;\r\n\r\n&lt;?php\r\n$parents_ids = get_post_ancestors($post-&gt;ID);\r\n$top_parent_id = (count($parents_ids) &gt; 0) ? $parents_ids[count($parents_ids)-1] : $post-&gt;ID;\r\nshow_all_children($top_parent_id, $post-&gt;ID, 1);\r\n?&gt;\r\n\r\n<\/pre>\n<p>Add the above code and check it . you can get the list of child pages titles in the sidebar.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial to show How to get child pages of specific parent page in sidebar dynamically Ex : Products is a parent page and Product-1,product-2,etc are child page(sub-pages) STEP 1 : To get child pages Add the below code in functions.php\u00a0 function show_all_children($parent_id, $post_id, $current_level) { $top_parents = array(); $top_parents = get_post_ancestors($post_id); $top_parents[] = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":153,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[6,14],"class_list":["post-28","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-child-page","tag-sidebar"],"_links":{"self":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":1,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions\/121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/media\/153"}],"wp:attachment":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}