• Resolved jumust

    (@jumust)


    Hi,
    in my website https://thingstodoincalifornia.info/southbay I have Category headers for each post and they show the Parent Category.
    Now only for “Events” Category I’d like headers to show the children categories separated by comma.
    Here is the code that actually I’m using

    <?php
     $just_shown = $last_shown; $last_shown = array(); $output = '';
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
     if(!in_array($catname,$just_shown)) {
     $cat = get_category($category->term_id); //new line to show only top level cat name
     if(!$cat->parent) { $output .= $catname . ' '; } //new if statement
     }
     $last_shown[] = $catname;
     }
     if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }
    ?>

    How to change it and accomplish what I want?

    Thanks

Viewing 15 replies - 1 through 15 (of 41 total)
  • I think this is what you want:

    if(!$cat->parent) {
       $output .= $catname . ' ';
       if ($cat->cat_name == 'Events') {
          $termids = get_term_children($cat->term_id,'category');
          $sep = '';
          foreach ($termids as $termid) {
            $childname = get_cat_name($termid);
             $output .= $sep . $childname;
             $sep = ', ';
          }
       }
    }
    Thread Starter jumust

    (@jumust)

    Thanks vtxyzzy!!!
    I have just a question:

    Should I merge your code with mine? Or just use your code?

    As I need to use

    $just_shown = $last_shown; $last_shown = array(); $output = '';
    and the output
    <div class="sectioncat">' . $output . '</div>';

    to accomplish this objective: have the category header above each post and if two posts belong to the same category, just show one time the header…you can check it out here

    Thanks

    Replace this:

    if(!$cat->parent) { $output .= $catname . ' '; } //new if statement
     }

    with the code I posted.

    Thread Starter jumust

    (@jumust)

    Sorry could you please send me all the code I need, because I don’t know where replace it.
    Just keep in mind that I need this line because I don’t want to show the category header if the previous post belongs to the same category check it out

    $just_shown = $last_shown; $last_shown = array(); $output = '';

    and I need the category header to be in the class “sectioncat”:

    if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }

    Thanks very much

    Here it is inside the code you first posted:

    <?php
     $just_shown = $last_shown; $last_shown = array(); $output = '';
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
     if(!in_array($catname,$just_shown)) {
     $cat = get_category($category->term_id); //new line to show only top level cat name
     if(!$cat->parent) {
       $output .= $catname . ' ';
       if ($cat->cat_name == 'Events') {
          $termids = get_term_children($cat->term_id,'category');
          $sep = '';
          foreach ($termids as $termid) {
            $childname = get_cat_name($termid);
             $output .= $sep . $childname;
             $sep = ', ';
          }
       }
    }
     $last_shown[] = $catname;
     }
     if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }
    ?>

    Thread Starter jumust

    (@jumust)

    It gives me this error
    Parse error: syntax error, unexpected T_ENDWHILE in /home/tipsand1/public_html/thingstodoincalifornia.info/wp-content/themes/diarise/index.php on line 209

    There’s a missing closing bracket.

    Fixed:

    <?php
     $just_shown = $last_shown; $last_shown = array(); $output = '';
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
        if(!in_array($catname,$just_shown)) {
            $cat = get_category($category->term_id); //new line to show only top level cat name
            if(!$cat->parent) {
                $output .= $catname . ' ';
                if ($cat->cat_name == 'Events') {
                    $termids = get_term_children($cat->term_id,'category');
                    $sep = '';
                    foreach ($termids as $termid) {
                    $childname = get_cat_name($termid);
                     $output .= $sep . $childname;
                     $sep = ', ';
                    }
                }
            }
        }
     $last_shown[] = $catname;
     }
     if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }
    ?>

    Beat me to it!

    Thread Starter jumust

    (@jumust)

    Thanks guys!
    ALMOST PERFECT!
    It grabs the sub-categories names only for the “Events” cat, but now it shows all the sub-categories names.
    Actually I need to show only the category/ies which the posts belong to
    Check it out

    Thanks so much

    I don’t have a way to test this, but I think it will work (assuming all this code is in the Loop!). Replace this foreach loop:

    foreach ($termids as $termid) {
       $childname = get_cat_name($termid);
       $output .= $sep . $childname;
       $sep = ', ';
    }

    with this:

    foreach ($termids as $termid) {
       if (in_category($termid)) {
          $childname = get_cat_name($termid);
          $output .= $sep . $childname;
          $sep = ', ';
       }
    }
    Thread Starter jumust

    (@jumust)

    Hi vtxyzzy,
    I test the code and this is what happened:
    – The category header say both parent and child i.e. Events Family Fun.
    – So two post that belong to Event parent category but different children will go above one header.

    Check it out

    And if post has two children they should be displayed separated by comma.

    Please let me know if I was not clear I know it’s tough.

    Thank you very much

    I am not sure that what you want can be done. What if a post belongs to sub-cats a and b, and the next one belongs to a and c, and the next one to b and d?

    Thread Starter jumust

    (@jumust)

    To make things easier only if all sub-categories are the same in both of posts the header is not displayed otherwise :

    a , b
    a, c
    b, c
    (b, c) in this case not shown

    Does it make sense?

    Not quite, because not showing b requires a look-ahead of two posts to figure out that b should not be shown. And since the number of posts is variable, you don’t know how far to look ahead. You could have a situation like this:

    a,b
    d,e
    f,g
    c,d
    b,c
    a,b

    There is no way to logically group these posts. I think you will have to settle for listing all subcategories, or none.

    Thread Starter jumust

    (@jumust)

    Yes you are right. But I was not so clear, I meant only if the PREVIOUS post belongs to one of the sub-categories the header should not be displayed. Could it work like it’s working now for the parents categories?

    Here is the code (I just added the comma for main cat)

    <?php
     $just_shown = $last_shown; $last_shown = array(); $sep = ''; $output = '';
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
        if(!in_array($catname,$just_shown)) {
            $cat = get_category($category->term_id); //new line to show only top level cat name
            if(!$cat->parent) {
                $output .= $sep . $catname; $sep = ', ';
                if ($cat->cat_name == 'Events') {
                    $termids = get_term_children($cat->term_id,'category');
                    $sep = '';
                    foreach ($termids as $termid) {
       if (in_category($termid)) {
          $childname = get_cat_name($termid);
          $output .= $sep . $childname;
          $sep = ', ';
       }
    }
                }
            }
        }
     $last_shown[] = $catname;
     }
     if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }
    ?>

    I.E.

    I have this 2 posts in 2 main categories: “Outdoors” and “Shopping” click here, they appear together below one header

    -Now I set “Chocolate Shop” post only to “Shopping” category:
    1 post: Shopping
    2 post: Shopping, Outdoors (it will be displayed only “Outdoors”) check it out

    -Unfortunately if I set the opposite: “Chocolate” to both of categories and “Wahoo” only to Shopping, I have:
    1 post: Shopping, Outdoors
    2 post: Shopping (it will not be displayed…it’s like if it belongs to both of categories, it doesn’t make sense…but it’s at least something; any thoughts?) here

    So it checks only the categories of the previous post and not further

    Could it work with the sub-categories of EVENTS: like “if is events” and after the same concept of main cateogries?

    THANKS SO MUCH for your time and help

Viewing 15 replies - 1 through 15 (of 41 total)
  • The topic ‘"If Category" function, get child category name’ is closed to new replies.