Wordpress category display child only one level - php

I'm trying to make a theme which shows an overview of child categories with title, link and description when entering a category archive. However, I only want to show child categories one level below the current category, and not the children of child categories.
How do I do that?
<?php
global $ancestor;
$childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
foreach ($childcats as $childcat) {
if (cat_is_ancestor_of($ancestor, $childcat->cat_ID) == false){
echo '<li><h2><a href="'.get_category_link($childcat->cat_ID).'">';
echo $childcat->cat_name . '</a></h2>';
echo '<p>'.$childcat->category_description.'</p>';
echo '</li>';
$ancestor = $childcat->cat_ID;
}
}
?>
I found that code, but it only returns one child. It returns. (Faa and Faq are child categories)
Faa
Faq -- Not displayed
Thanks!

Make sure 'FAQ' has post under it. If you notice the "code you found" is passing the parameter "&hide_empty=1" which means it will not return categories that are empty.
So your options are to either remove that or make sure your category has posts under it.

Related

Print only parent categories of post in custom RSS feed

I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed.
Using <?php the_category_rss(); ?> prints all categories for a post (parent and child categories) like this:
<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>
I'd like to print only the parent categories for a post rather than parent and child categories.
I'm also looking for the proper way to print the parent category's permalink within the RSS feed.
I have a feeling the way to do this is a filter, but I'm not sure how to implement it. This is what I have right now, but it returns a 1 instead of the parent category name:
add_filter('the_category_rss', 'only_parent_rss_categories');
function only_parent_rss_categories( $allparents ) {
$categories = get_the_category();
$category = $category->category_parent == 0;
$allparents= esc_html("<category><![CDATA[{$category}]]></category>");
return $allparents;
}
I'm still interested in hearing how this can be accomplished using the Wordpress function the_category_rss(), but I did find another way to do this:
<?php $parentsonly = "";
// loop through categories
foreach((get_the_category()) as $category) {
// exclude child categories
if ($category->category_parent == 0) {
$parentsonly = '<category domain="' . get_category_link($category->cat_ID) . '">' . $category->name . '</category>';
}
}
echo $parentsonly; ?>
This returns each parent category in the proper RSS validated format, using <category> and domain for the category URL:
<category domain="http://www.yourdomain.com/category-archive/">My Category</category>
Hope this helps someone else!

Display categories list in Wordpress without subcategories

I am attempting to show categories in a list, but without displaying subcatgories. My current code is:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&show_count=0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
}?>
Which displays the categories nicely
but when I added a sub-category it looked like this:
Any ideas? Thanks!
Use get_categories() instead.
https://developer.wordpress.org/reference/functions/get_categories/#Get_only_top_level_categories
It has "parent" parameter which you can set to 0 and get desired result.
looks like a css problem to me : you probably want to hide the subcategory item until it's clicked or hovered

Check if category of post is x or is child of x

My wordpress site has categories with a simple hierarchy –
Blog
Role Shift
Urod the Last Show
News
I'm using the if statement below to catch if the post is in category blog or any of the child categories - and it works - but it feels stupid not to be able to just check the parent of the current category (plus I might want to add categories later).
if ( is_category('blog') || in_category(array('role-shift', 'urod-the-last-show', 'news')) )
I've searched and tried every suggestion - including cat_is_ancestor_of - but nothing is working.
Please help!
Robert
$categories = get_the_category(); //returns categories
$thiscat = $categories[0];
$parent_id = $thiscat->parent; //the parent id
$parent = get_category($parent_id) //this returns the parent category as an object
//use id or slug of category you are searching for
if( $parent_id == 1 || $thiscat->slug == 'blog' ){
//this is a child of blog or the blog page
}
This should do the trick.
This will determine if the current category is a child of the blog page.
The first part, get_category, returns the current category.
Then you can get the parent id from the current category and use 'get_the_category_by_ID' to get the parent category object.
Then you can check if you are under the parent category you want.

Hiding ONE category name within Wordpress

I have a category within my Wordpress build that I need to exclude, the category is named 'portfolio' which is being pulled in on another page, but I need the name of that category to be hidden but show any other category names a post is under... I have searched everywhere and found nothing. I also found a plugin but it hasn't been updated for 2 years so I am wary of it.
Assuming the categories are being displayed from within the loop, here is some code that will display all of the categories the post is in, with links to the categories. "Portfolio" should be excluded.
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
if ($category['slug'] != 'portfolio') {
$output .= ''.$category->cat_name.''.$separator;
}
}
echo trim($output, $separator);
}
?>
Just replace your current code for displaying the categories with this. Note that this will not affect you site-wide; you will have to manually replace all instances where categories are displayed.

Magento - browse 4 tier category tree

I'm trying to build some code on my magento site that will allow the customer to click through my product categories viewing the child categories on each consecutive page.
I have a code snippet below that will successfully works for the first 2 levels, but does not allow me to browse the childern of the second level (i.e.the 3rd level etc)
Can anyone assist in tweaking this code to allow me to browse 4 levels deep?
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
}
echo "</ul>\n</li>\n";
?>
You need to add recursively the $obj->getCurrentChildCategories() for the child categories aswell, since your current foreach only iterates for the childs of the first one.
Store the foreach in a function and use it for the $cat object.
function cats($obj) {
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
cats($subcat);
}
}

Categories