• 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 15 replies - 1 through 15 (of 22 total)
  • Should I use a function to grab the previous post category ID and check if the ID is the same?

    that is probably a good way to deal with the challenge.
    it might not need a function – maybe a variable or array to store the category(ies) of the previous post, and then compare/exclude them in the header of the next post.

    can you post the code of the template that creates this page?

    (paste the code into a https://wordpress.pastebin.com/ and post the lonk to it here)

    working with the existing code, it might be easier to make a suggestion; otherwise everythig is guesswork.

    Thread Starter jumust

    (@jumust)

    THANKS SO MUCH Alchymyth!!!
    I think your suggestion to use a variable or array is totally right. But actually I tried, tried…and I can’t figure out this code.
    I posted the template here https://wordpress.pastebin.com/iqdaTzTi
    and I highlighted the initial part of the tag “box” (within the “box” there is the single post) where you can see the “foreach” to grab the category name.
    I hope I was clear, I’m wondering if you can give me other of your valuable suggestions.
    Thanks

    this is to replace some of the highlighted part:

    <div class="sectioncat">
     <?php
     $just_shown = $last_shown; $last_shown = array();
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
     if(!in_array($catname,$just_shown)) { echo $catname . ' '; }
     $last_shown[] = $catname;
     }
     ?>
     </div>

    and those two new arrays need to get initialized in line 30:
    change from:

    <?php if (have_posts()) : $count = 0; ?>

    to:
    <?php if (have_posts()) : $count = 0; $just_shown = array(); $last_shown = array(); ?>

    principle:
    the two arrys $just_shown and $last_shown start empty;

    whenever the list of categories (you only use one, but in principle it could be many) is output, they get stored into the array $last_shown;

    next post, this array is transferred to the array $just_shown which then contains the categories which were shown for the previous post – if they appear again in this post, they will not be displayed;
    all the categories of this post will get stored in $last_shown;

    and so on.

    the result for a sequence of posts with just one category each, for example, would be:

    shopping - display
    events - display
    events - no display
    events - no display
    shopping - display
    shopping - no display
    events - display

    Thread Starter jumust

    (@jumust)

    That’s GREAT! Thanks for the code and for your explanation, it’s awesome!
    It works perfectly, You ROCK!
    Thank you so much

    Thread Starter jumust

    (@jumust)

    Hi,
    I’m wondering if you can help me again because now I’m in trouble when I change your code.
    In the home page I’d like to show only the parent category name:
    https://www.screencast.com/users/JuriT/folders/Jing/media/90c5613d-84ce-4367-aeb4-3f9769318308
    EX. Medical–>Chiropractic or Shopping–>Bookstores. I want to show only Medical and only Shopping.
    If I change your code with get_category_parents it doesn’t work.

    The other thing regards the Parent Category page. So if I click on Medical Category, in this page I’d like to display the header only with the child category name (or two separated with comma if the post belong to two child categories.) https://www.screencast.com/users/JuriT/folders/Jing/media/1f770215-2719-4c06-ab4a-2709b700bb79

    The code is almost the same for the Home page and Category Pages so I think I should change this part of your code:

    foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;

    RIGHT?

    <?php
     $just_shown = $last_shown; $last_shown = array();
     foreach((get_the_category()) as $category) {
     $catname = $category->cat_name;
     if(!in_array($catname,$just_shown)) { echo $catname . ' '; }
     $last_shown[] = $catname;
     }
     ?>

    Thanks for all your help.

    this should work in the home page:
    (it will only show the category name, if the category does not have a parent; i.e. only top level category name)

    <?php
     $just_shown = $last_shown; $last_shown = array();
     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) { echo $catname . ' '; } //new if statement
     }
     $last_shown[] = $catname;
     }
     ?>

    ————–
    the one with the Parent Category page is not too clear to me –
    if the following does not work –
    can you describe your category structure in more detail, or post a link to the site so i can check the categories?

    you could try:

    <?php
     $just_shown = $last_shown; $last_shown = array(); $sep = '';
     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) { echo $sep . $catname; $sep = ', '; }
     }
     $last_shown[] = $catname;
     }
     ?>

    it is checking if the category is a parent cat, and if so, the cat name will not be shown.

    Thread Starter jumust

    (@jumust)

    AWESOME!!!
    It looks like is working very well, but I’m testing it out again….if I have any doubts I’ll ask you.
    Anyway if you’d like to take a look, the link to the site is https://thingstodoincalifornia.info/southbay
    Actually the category structure is a little confused because I’m still working on it and thinking what’s the best structure.

    I really appreciate your help. You were pretty kind and helpful. Hope there are a lot of people like you.

    THANK YOU VERY MUCH

    you are welcome ??

    the world is full of good people

    Thread Starter jumust

    (@jumust)

    Hey Alchymyth,
    may you edit just a bit your code to show the category header only if it’s the same than previous category, because I have some problem with css. https://www.screencast.com/users/JuriT/folders/Jing/media/d5ca7c8e-b9ae-4d4a-86ac-b92f915d56b3

    this is the code in my template

    <div class="sectioncat">
     <?php
     $just_shown = $last_shown; $last_shown = array();
     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) { echo $catname . ' '; } //new if statement
     }
     $last_shown[] = $catname;
     }
     ?>
     </div>

    Thanks so much

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

    (this will ‘collect’ the category output into a variable, instead of immedeately echoing it; it will only echo the div with the category name if there is a mname to output)

    (untested – make a backup copy of your working theme files before editing)

    Thread Starter jumust

    (@jumust)

    Thanks so much!
    I tried the code but it looks like there is an error…

    Here is what it displays in the browser:

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

    Any ideas?

    I appreciate all your help.

    Thanks

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

    Try thi once.

    Thread Starter jumust

    (@jumust)

    Sounds Perfect!!!
    alchymyth just missed a }

    Thanks Guys

    Thread Starter jumust

    (@jumust)

    How about the Parent Category Page?
    Here is the original code:

    <?php
     $just_shown = $last_shown; $last_shown = array(); $sep = '';
     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) { echo $sep . $catname; $sep = ', '; }
     }
     $last_shown[] = $catname;
     }
     ?>

    I tried to change the code with this one:

    <?php
     $just_shown = $last_shown; $last_shown = array(); $sep = '';
     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;
     }
     if($output != '') {
      echo '<div class="sectioncat">' . $output . '</div>';
     }
    ?>

    But it repeats the Cat Header even though posts belong to the same cat.

    Thanks

    nearly perfect;

    you missed the initialisation – to set $output = ''; – in the first line:

    $just_shown = $last_shown; $last_shown = array(); $sep = ''; $output = '';
Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘If the post belongs to the previous Category – no cat name’ is closed to new replies.