Forums

[resolved] "If Category" function, get child category name (42 posts)

  1. jumust
    Member
    Posted 1 year ago #

    Hi,
    in my website http://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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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 = ', ';
          }
       }
    }
  3. jumust
    Member
    Posted 1 year ago #

    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

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Replace this:

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

    with the code I posted.

  5. jumust
    Member
    Posted 1 year ago #

    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

  6. vtxyzzy
    Member
    Posted 1 year ago #

    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>';
     }
    ?>
  7. jumust
    Member
    Posted 1 year ago #

    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

  8. Matthew Muro
    Member
    Posted 1 year ago #

    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>';
     }
    ?>
  9. vtxyzzy
    Member
    Posted 1 year ago #

    Beat me to it!

  10. jumust
    Member
    Posted 1 year ago #

    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

  11. vtxyzzy
    Member
    Posted 1 year ago #

    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 = ', ';
       }
    }
  12. jumust
    Member
    Posted 1 year ago #

    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

  13. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  14. jumust
    Member
    Posted 1 year ago #

    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?

  15. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  16. jumust
    Member
    Posted 1 year ago #

    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

  17. vtxyzzy
    Member
    Posted 1 year ago #

    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
  18. jumust
    Member
    Posted 1 year ago #

    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

  19. vtxyzzy
    Member
    Posted 1 year ago #

    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
  20. jumust
    Member
    Posted 1 year ago #

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

  21. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  22. jumust
    Member
    Posted 1 year ago #

    Thank you very much!
    Here is the code

  23. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  24. jumust
    Member
    Posted 1 year ago #

    sorry here you are click

  25. vtxyzzy
    Member
    Posted 1 year ago #

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

  26. jumust
    Member
    Posted 1 year ago #

    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

  27. vtxyzzy
    Member
    Posted 1 year ago #

    Alright, change this line:

    $curr_parents[] = $catname;

    to this:

    if ($catname != $event_name) $curr_parents[] = $catname;
  28. jumust
    Member
    Posted 1 year ago #

    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...

  29. vtxyzzy
    Member
    Posted 1 year ago #

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

  30. jumust
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic