• Resolved Rafael_Magnvs

    (@rafael_magnvs)


    I have found the snnipet below and I want to do achieve two things:

    1. Keep it from showing the current post category. In other words, show all the sibling categories except the category-name of the given post

    2. I have used a comma as a separator, but how can I keep it from showing after the last item of the list?

    Find the code below:

    <?php
    
    	global $post;
    
    	$cats = get_the_category($post->ID);
    
    	$catid = $cats[0]->cat_ID;
    
    	while ($catid) {
    
    	  $cat = get_category($catid); 
    
    	  $catid = $cat->category_parent; 
    
    	  $catParent = $cat->cat_ID;
    
    	}
    
    	$categories = get_categories('child_of='.$catParent);
    
    	foreach ($categories as $category) {
    		echo '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.', </a>';
    
    	}
    ?>

    Any thoughts would be greatly appreciated .. Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • For 1) I think you could use ‘exclude’ in the ‘get_categories’ call.

    $categories = get_categories(array('child_of' => $catParent, 'exclude' => $catid ));

    For 2) You could use an array variable and ‘implode’:

    $categories = get_categories('child_of='.$catParent);
    $catlinks = array();
    	foreach ($categories as $category) {
    		$catlinks[] = '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a>';
    
    	}
    $commasep = implode("," , $catlinks);

    The above code is untested but I think it should do the trick.

    Thread Starter Rafael_Magnvs

    (@rafael_magnvs)

    Hey BytheGram, Thanks for the prompt response.

    I have tried both of your suggestions and it does seem t be working.

    $categories = get_categories(array('child_of' => $catParent, 'exclude' => $catid ));

    For whatever reason, the above code doesn’t change anything, all the children categories are still being displayed as before..

    In regards to the second part, I placed the code and instead of removing the last comma, it made all categories disappear. I dont know why tour snnipet didnt’t work, but I appreciate you taking the time to take a look at my problem.

    Do you have any ideas on why it did not work as you predicted?

    I didn’t have a chance to actually test the code, I could have missed something. I’ll test it now for you and reply in a bit, hopefully with a solution.

    Edit:

    I forgot to echo $commasep. That is why nothing is showing up.

    sorry let me do some more work before posting half finished code. My bad

    Thread Starter Rafael_Magnvs

    (@rafael_magnvs)

    I “echoed” as you suggested and it did the trick the list is there again and separated by commas.. However, the last category still have comma, which puts us back to the original issue.

    From what I can tell the array created by get_categories (or even wp_list_categories which I later use) always ended with an empty key which is why the string had a comma at the end. Here is the code that I ended up using that I’m pretty sure should work to solve both issues, give it a try and let me know. I’m testing on a pretty basic setup with minimal categories to work with so I can only tell that it does work for me.

    $categories = get_the_category($post->ID);
    $catlinks = array();
    $parent = $categories[0]->category_parent;
    $siblingsstring = wp_list_categories( array ('parent' => $parent, 'exclude' => $categories[0]->cat_ID, 'style' => 'none', 'echo' => false ));
    $siblings = explode("<br />", $siblingsstring );
    $removelast = array_pop($siblings);
    echo implode(',', $siblings);
    Thread Starter Rafael_Magnvs

    (@rafael_magnvs)

    BytheGram… that worked like there is no tomorrow!

    Thank you for all your effort, I achieved exactly what I was going after. It is perfect!

    More power to you mate!

    No worries! Glad I could help, loved the challenge.

    All the best to ya and happy coding!

    Thread Starter Rafael_Magnvs

    (@rafael_magnvs)

    This is solved!

    Any ideas how to make this work with custom post types categories and sub-categories siblings?

    Moderator bcworkz

    (@bcworkz)

    @jampafoo, next time, please start your own topic. More people will read you question as questions with no replies are much more likely to be read. The mods here also prefer you do this.

    To answer your question, see Template Tags/wp list categories. Supply your custom post type’s category taxonomy as the ‘taxonomy’ argument. All child categories are displayed by default, but can be managed by the ‘depth’ argument.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to display/list the post's sibling categories, but hide the current post cat’ is closed to new replies.