• Resolved Rose

    (@eos-rose)


    In my single.php file I am trying to list all the child categories under a specific parent category. I’m using the following code:

    if(!function_exists('subcat')){
    	function subcat(){
    		global $post;
    foreach((get_the_category()) as $category)
    {
        if ($category->category_parent == "177")
        {
            echo '<a href="' . get_category_link($category->cat_ID) . '">' . $category->cat_name . '</a>';
        }
    }
    	}
    }

    The code works, but in instances where there are multiple subcategories I would like to separate those subcategories with a comma. How do I do that? Anyone know?

Viewing 2 replies - 1 through 2 (of 2 total)
  • This should work:

    replace this
    if ($category->category_parent == "177")

    with this

    $cats = array(177, 213, 309);
    if ( in_array( $category->category_parent, $cats ) ) {

    if you have one category you can either use
    if ($category->category_parent == "177")
    or

    $cats = array(177);
    if ( in_array( $category->category_parent, $cats ) ) {

    Thread Starter Rose

    (@eos-rose)

    Thank you. I actually figured out an alternative using css. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Comma between subcategory list’ is closed to new replies.