• I want to display a dynamic list of page title links when you are on a page that has sub pages. I found out that the code below works for pages.

    <script src="https://pastebin.com/embed_js.php?i=CuQuLsua"></script>

    How do I get it to work for categories?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter azukah

    (@azukah)

    Ok, am new here. I obviously don’t get how things work.
    I guess this is the code below:

    <?php if ( is_page() ) {
          if($post->post_parent)
          $children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
    else
          $children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
          if ($children) {
       ?>
          <div class="sidebar">
    
    <ul>
            <?php echo $children; ?>
            </ul>
          </div>
       <?php
    } // End If Post
    
           } // End if is page
       ?>

    How can I get this to work on Categories instead of Pages?

    You’re trying to list sub-catgories on a category archive page? Try this:

    if(is_category()) :
    	$breakpoint = 0;
    	$thiscat = get_term( get_query_var('cat') , 'category' );
    	$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
    
    	if(empty($subcategories) && $thiscat->parent != 0) :
    		$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
    	endif;
    	$items='';
    	if(!empty($subcategories)) :
    		foreach($subcategories as $subcat) :
    			if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
    			$items .= '
    			<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
    				<a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.'</a>
    			</li>';
    		endforeach;
    		echo "
    		<!--START SUB CATEGORY -->
                   <div id='subcats'>
    		<ul>$items</ul>
    		</div><!--/subcats--> ";
    	endif;
    	unset($subcategories,$subcat,$thiscat,$items);
    	endif;

    You’ll have to style it using CSS.

    Thread Starter azukah

    (@azukah)

    Thanks but does this work for Pages too? Am displaying the list on the siderbar.

    It might be easier if you search the plugin directory for a plugin that can create sub-menus or list sub-categories.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List Children of Category Only On That Parent Category’ is closed to new replies.