Forums

[resolved] Comma between subcategory list (3 posts)

  1. Eos Rose
    Member
    Posted 11 months ago #

    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?

  2. stvwlf
    Member
    Posted 11 months ago #

    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 ) ) {
  3. Eos Rose
    Member
    Posted 11 months ago #

    Thank you. I actually figured out an alternative using css. :)

Reply

You must log in to post.

About this Topic