Forums

[resolved] If the post belongs to the previous Category - no cat name (23 posts)

  1. jumust
    Member
    Posted 1 year ago #

    Hi,
    this is the website I'm working on http://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!!!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    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 http://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.

  3. jumust
    Member
    Posted 1 year ago #

    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 http://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

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

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

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

  6. jumust
    Member
    Posted 1 year ago #

    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:
    http://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.) http://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.

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    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.

  8. jumust
    Member
    Posted 1 year ago #

    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 http://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

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    you are welcome ;-)

    the world is full of good people

  10. jumust
    Member
    Posted 1 year ago #

    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. http://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

  11. alchymyth
    The Sweeper
    Posted 1 year ago #

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

  12. jumust
    Member
    Posted 1 year ago #

    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

  13. MAS
    Member
    Posted 1 year ago #

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

  14. jumust
    Member
    Posted 1 year ago #

    Sounds Perfect!!!
    alchymyth just missed a }

    Thanks Guys

  15. jumust
    Member
    Posted 1 year ago #

    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

  16. alchymyth
    The Sweeper
    Posted 1 year ago #

    nearly perfect;

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

    $just_shown = $last_shown; $last_shown = array(); $sep = ''; $output = '';
  17. jumust
    Member
    Posted 1 year ago #

    PERFECTTTTT!!!!!
    THANKS AGAIN

  18. jumust
    Member
    Posted 1 year ago #

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

  19. jumust
    Member
    Posted 1 year ago #

    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

  20. alchymyth
    The Sweeper
    Posted 1 year ago #

    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 http://.../southbay/category/events/ ) i.e. 'Events, Music' ), or in the line below the main category?

  21. jumust
    Member
    Posted 1 year ago #

    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!

  22. jumust
    Member
    Posted 1 year ago #

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

    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

Topic Closed

This topic has been closed to new replies.

About this Topic