• 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 - 16 through 30 (of 41 total)
  • I am trying to understand exactly what you want to do. Suppose you have the following category structure:

    p1
       c1a
       c1b
    p2
       c2a
       c2b

    What do you want to show for each of the following:

    p1                Header p1
    p1                No header
    p1, p2            Header p1, p2 ?
    p1
    p1, c1a
    p1, c1b
    p1, c2a
    p2, c2a
    p2, c2a, c2b
    p2, c2b
    p2, c2b, c1a
    Thread Starter jumust

    (@jumust)

    Actually I don’t need anytime to select 2 parents:
    The rule is: 1 Parent, unlimited children.
    If they are in that order ideally I’d like to show:

    p1            Header p1
    p1            No Header
    p2            Header p2
    p1            Header p1
    p1, c1a       No Header (for all categories show only the parent, except for Events, so no header because p1 is the same of the previous post)
    p1, c1b       No Header
    p1, c2a       No Header
    p2, c2a       Header p2
    p2, c2a, c2b  No Header
    p2, c2b       No Header
    p1, c2b, c1a  Header p1

    I.e.
    Events is
    p3
    c3a
    c3b

    and for Events always will be selected at least one child so:

    p1             Header p1
    p3, c3a        Header c3a
    p3, c3b        Header c3b
    p3, c3a, c3b   Header c3a, c3b
    p3, c3b        Header c3b
    p3, c3b        No Header
    p2, c2a        Header p2
    p3, c3a        Header c3a
    p1, c1a        Header p1
    p3, c3a, c3b   Header c3a, c3b
    p3, c3a, c3b   No Header

    So the main meaning should be:
    For all categories show only the parent, so check only if the previous post belongs to the same parent, if so don’t show the header:

    p1, c1a, c2b         Header p1
    p1, c2a, c2b         No Header
    p2, c1a              Header p2
    p1, c1a, c2b         No Header

    Except for Events: Only if the previous post belongs to all the same children, the header should not be displayed:

    p3, c3a, c3b         Header c3a, c3b
    p3, c3a              Header c3a
    p3, c3b              Header c3b
    p3, c3a, c3b         Header c3a, c3b
    p3, c3a, c3b         No Header
    p3, c3a              Header c3a
    p3, c3a              No Header

    Hope I was clear now ??

    Thanks a lot

    I think this will do what you want. The logic is:

    If ((any parent is different)
           OR (one of the parents is 'Events' AND any Events children are different))
       create the header
    if (have_posts()) {
      $curr_parents = array('force first header');
      $curr_event_children = array();
      $event_name = 'Events';
      while (have_posts()) {
        the_post();
    
        //  Start of code to create headers
        $prev_parents = $curr_parents; $curr_parents = array();
        $prev_event_children = $curr_event_children; $curr_event_children = array();
        // Get the current parents and Event children (if any)
        foreach((get_the_category()) as $category) {
          $catname = $category->cat_name;
          if ($catname == 'Uncategorized') continue;
          if (!$category->parent) {
            $curr_parents[] = $catname;
            if ($catname == $event_name) {
              $termids = get_term_children($category->term_id,'category');
              foreach ($termids as $termid) {
                if (in_category($termid)) $curr_event_children[] = get_cat_name($termid);
              }
            }
          }
        }
        // Now, check for differences
        sort($curr_parents);
        sort($curr_event_children);
        $show_header = 0;
        if ($curr_parents != $prev_parents) {
          ++$show_header;
        } elseif (in_array($event_name,$curr_parents) &&
          ($curr_event_children != $prev_event_children)) {
          ++$show_header;
        }
        if ($show_header) {
          $output = implode($curr_parents,', ');
          if ($curr_event_children) $output .= ', ' . implode($curr_event_children,', ');
          echo '<div class="sectioncat">' . $output . '</div>';
        }
        $prev_parents = $curr_parents;
        $prev_event_children = $curr_event_children;
        // End of code for headers
    Thread Starter jumust

    (@jumust)

    Sorry if I’m not so good with functions, but it supposed to have <php at the beginning and close it at the end?

    I can’t tell because I can’t see the rest of your template. It has to fit correctly into your code. If you will post the WORKING template into the pastebin, and post the link to it here, I will fit it in.

    Thread Starter jumust

    (@jumust)

    Thank you very much!
    Here is the code

    Sorry, but I need the WORKING template that was used to create the example screenshots you showed earlier – the ones with the headers in them.

    Thread Starter jumust

    (@jumust)

    sorry here you are click

    OK – here is the merged code. I have no way to test it, but I think it is correct.

    Thread Starter jumust

    (@jumust)

    ALMOST PERFECT!
    It works but just show the Event main cat too in the header( It’s like “Events, Music” but should be “Music”)
    Look at here two posts belong to Events–>Music and the header is not displayed in the second one, great!

    Probably you did it to show the Event Header in posts that belong only to main category Events, but never I’ll select only Events, I’ll use at least one sub-cat.

    Thanks

    Alright, change this line:

    $curr_parents[] = $catname;

    to this:

    if ($catname != $event_name) $curr_parents[] = $catname;
    Thread Starter jumust

    (@jumust)

    mmm….before I changed that line I had this
    Now I have this
    The header is only displayed in the first one and there is a comma…

    I will try again. It is difficult because I cannot test the complete code.

    Thread Starter jumust

    (@jumust)

    I understand you, if you want I can create a login for you and send you over.
    Please just let me know

    Take out the change I posted earlier – change this:

    if ($catname != $event_name) $curr_parents[] = $catname;

    back to this:

    $curr_parents[] = $catname;

    and then change this:

    if ($show_header) {
          $output = implode($curr_parents,', ');
          if ($curr_event_children) $output .= ', ' . implode($curr_event_children,', ');
          echo '<div class="sectioncat">' . $output . '</div>';
        }
    if ($show_header) {
           // Take $event_name out of parents array
           $temp_parents = array_diff($curr_parents,array($event_name));
           $output = implode($temp_parents,', ');
           if ($curr_event_children) {
             if ($output) $output .= ', ';
             $output .= implode($curr_event_children,', ');
           }
           echo '<div class="sectioncat">' . $output . '</div>';
        }
Viewing 15 replies - 16 through 30 (of 41 total)
  • The topic ‘"If Category" function, get child category name’ is closed to new replies.