• Resolved jumust

    (@jumust)


    Hi,
    this is the website I’m working on https://www.screencast.com/users/JuriT/folders/Jing/media/330e211e-234f-47c4-921c-bc23252af4c7

    I’d like to add some code to accomplish this objective but I really have no idea:
    As you can see in the screenshoot there are 3 posts which belong to this categories: Shopping, Events. Because the last two belong to the same category (events) I don’t want to show the category header for the last one. So what i want is to get two posts listed under one Header like EVENTS.

    Should I use a function to grab the previous post category ID and check if the ID is the same?
    I’m really in trouble.
    Thanks so much for your help!!!

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter jumust

    (@jumust)

    PERFECTTTTT!!!!!
    THANKS AGAIN

    Thread Starter jumust

    (@jumust)

    Hi Alchymyth,
    I’m again here to ask you a hand if it’s possible.
    I need to make a couples of little tweaks in the code you kindly sent me.

    In the Home Page I have this code:

    <?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>';
     }
    ?>

    I’d like to use an if…else statement in the home page I guess, in order to have posts that belong to “Events” category show only children categories separated by comma in the header (and never show the header “Events”).

    So I tried to use <?php if (in_category(‘events’)) : ?> and edit your code but I got lost.

    Here is what I did:

    <?php if (in_category('events')) { ?>
    echo '<?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_categories('parent='.$category->term_id);
     if(!$cat) { $output .= $sep . $catname; $sep = ', '; }
     }
     $last_shown[] = $catname;
     }
     ?>'
    else
    echo '<?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>';
     }
    ?>' <?php } ?>

    Thanks for your help!!!

    Thread Starter jumust

    (@jumust)

    Hi Alchymyth,
    please let me know I can hire you for that.
    This is my request: 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 you sent me last time:

    <?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>';
     }
    ?>

    Thanks for all

    do you want to show all child categories of ‘Events’ every time a post of the ‘Events’ category is shown?

    or just the child category that belongs to that particular post?

    do you want the child categories for ‘Events’ to appear in the same line (as in https://…/southbay/category/events/ ) i.e. ‘Events, Music’ ), or in the line below the main category?

    Thread Starter jumust

    (@jumust)

    Hi Alchymyth,
    thanks for your help.

    What I’d like is show the child category that belongs to that particular post and Never show the parent “Events” (and if I select two or more children I’d like these to be separated by comma)

    So If I have a post in Parent “Events” and Children “Music” and “Local Events” I’d like to see in the header of the post:
    “Music, Local Events”

    This should be only for “Events” category and only for the home page.

    Please let me know if you need more info.

    Thanks very much!

    Thread Starter jumust

    (@jumust)

    How about this?
    It gives me error but looks good:

    <?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)

    Hi Alchymyth,
    check it out this, vtxyzzy member is trying to figure it out. I’m wondering if you can let me know your thoughts.
    Thanks

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘If the post belongs to the previous Category – no cat name’ is closed to new replies.