• Resolved destinedjagold

    (@destinedjagold)


    Hello and good day.
    I wish to ask for help with this code that I’ve found while searching around. Basically, I wanted the category widget to display only the child categories in the sidebar, but since I couldn’t find how to do so (by adding some filter codes in the functions.php), I’ve decided to try a custom category code that I’ve placed in the sibebar.php

    Here is the code that I am using.

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    <?php
    global $post;
    if (is_category( )) {
      $cat = get_query_var('cat');
      $parent = get_category ($cat);
      if ($parent->parent) {
        wp_list_categories ('child_of=' . $parent->parent);
        } else {
        wp_list_categories ('child_of=' . $cat);
      }
    }
    ?>

    It actually works in the archive and category pages, but my problem is that it doesn’t show up when I’m on the post page.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • http://codex.wordpress.org/is_category

    That is the big “if” in your code.

    Thread Starter destinedjagold

    (@destinedjagold)

    Ah, yes. I have forgotten to post that I have already fixed this little issue of mine. Pardon me for being a newbie with WordPress though, but I did understand that is_category was for the category/archive pages, hence why it won’t work for the single post page.
    With that said, I researched about it and stumbled upon is_single. I then screwed around with the code and finally, I solved my simple problem which was annoyingly not so simple for a newbie like myself. 😛

    Here is my code for those who are curious~

    <?php
    if (is_category( )) {
    	$cat = get_query_var('cat');
    	$parent = get_category ($cat);
    
    	if ($parent->parent) {
    		wp_list_categories ('child_of=' . $parent->parent);
    	} else {
    		wp_list_categories ('child_of=' . $cat);
    	}
    } else if(is_single( )) {
    	$getcategory = get_the_category() ;
    	$parentcatid = $getcategory[0]->category_parent;
    
    	wp_list_categories ('child_of=' . $parentcatid);
    }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Category List In Sidebar’ is closed to new replies.