How to display sub categories in wordpress - php

I'm trying to display Sub categories on main category page.
For example i have a category named as : live tv
And it is consist of some sub categories like : sports tv, cartoon tv, entertainment.
I want to display these sub categories with images inside content area of main category.
I was able to assign images to categories using plugin.
I have tried this code in category.php
<?php
if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
But that code isn't showing anything on my category page.

you can also get sub category as:
<?php global $wpdb;$prefix=$wpdb->prefix;
$subcateogyr_list=$wpdb->get_results("Select * from ".$prefix."term_taxonomy WHERE parent='parent_category_id'");
foreach($subcateogyr_list as $subcat
echo $subcat_name=$wpdb->get_var("select name from ".$prefix."wp_terms where term_taxonomy_id='$subcat['term_id']'");
}
?>

Related

Php inserting just main category

This code inserting main and sub categories.
<?php
foreach((get_the_category()) as $category){
$cat_link = get_category_link($category->cat_ID);
echo ''.$category->name.'';
}
?>
i want just main one category name.

Show sub category listing on category page in Porto theme Magento

I tried to show Sub category listing on category page in Porto theme, but nothing works in Magento. I also tried this tutorial.
You have to go to app/design/frontend/theme/template/catalog/product/list.phtml.
This is your category listing page.
Here you can put the code
<?php
$currCategoryId = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($currCategoryId);
$subcategories = $category->getChildrenCategories();
if(count($subcategories)>0){
$count=1;
foreach ($subcategories as $subcategory) {
$category1 = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategories1 = $category1->getChildrenCategories();
echo $subcategory->getName()."<br/>";
if(count($subcategories1)>0){
foreach ($subcategories1 as $thirdLevelCategory) {
echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
echo $thirdLevelCategory->getName()."<br/>";
}
}
}
}
?>
Note: It's better to create a block and load your data from the block or you can create a helper in your module and use that in your template.

Showing subcategory articles under headings for blog layout

We are setting up a custom blog layout override for a Joomla site. I am trying to display the sub categories articles under their category headings.
For example the following structure:
Main Cat
- Sub Cat
-- Article 1
-- Article 2
- Sub Cat
-- Article 3
This layout is how the articles should display on the main blog override. For example going to Main Cat would display the above layout.
The subcategories are included in the default way with the following:
<?php if (!empty($this->children[$this->category->id]) && $this->maxLevel != 0) : ?>
<div class="cat-children">
<?php if ($this->params->get('show_category_heading_title_text', 1) == 1) : ?>
<h3> <?php echo JText::_('JGLOBAL_SUBCATEGORIES'); ?></h3>
<?php endif; ?>
<?php echo $this->loadTemplate('children'); ?> </div>
<?php endif; ?>
In the child override the title is included then we would like to display the articles from that child category.
I have tried multiple different versions of getting a for each to work for the sub articles but cannot get it to work.
For example:
<?php foreach ($this->child->children[$child->id] as $key => $item) : ?>
1
<?php echo $item->title; ?>
<?php endforeach; ?>

Remove parent item in a sub category list (wp_list_categories)

UPDATE:
Nevermind, stupid error: it wasn't actually showing the parent title, it was a sub category with the same name.
I have the following list to display the child categories of the current category and I'd like to remove or hide the parent item, showing only the children. Is it possible without writing a function in functions.php?
HTML:
<div class="tabs">
<ul>
<?php $this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1; ?>
<?php $this_category = get_category($this_cat);
if ( $this_category->parent ) { $this_cat = $this_category->parent; } ?>
<?php wp_list_categories('child_of=' . $this_cat . '&title_li='); ?>
</ul>
</div>
RESULT:
"issue of the year" is the parent which I want to remove
Thanks,
If you have id of category parent ,you can use get_term_children
Ex:
$chlidrenCategories = get_term_children( categoryParentId ,'Category');
Nevermind, stupid error: it wasn't actually showing the parent title, it was a sub category with the same name.

Show wordpress subcategories only

I have 7 Categories (parents), and every category has 15 sub-categories.
When I select some category (parent), I want to display only sub-categories (children) of that particular parent category (parent).
After I click on sub-category (child) then it should display its posts only.
I have a fron_page.php and category.php.
How can I write this to first show sub-categories separately, then post of that sub-category separately in new file, which user want to see.
This code should help you:
<ul>
<?php
$cats = get_the_category();
$mycat = $cats->cat_ID;
wp_list_categories('orderby=id&child_of='.$mycat);
?>
</ul>
OR
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<ul>'.$this_category.'</ul>';
}
}
?>
Let me know please.
Good luck! :)
1) Showing only Subcategories:
<?php
// if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
if ($cur_cat)
{
$new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&&show_count=1&hide_empty=0');
echo '<ul>' . $new_cats . '</ul>';
}
}
?>
2) Showing All Top Categories:
<?php
wp_list_categories('depth=1&title_li=&exclude=1&show_count=1&hide_empty=0');
?>
3) Showing All Top Categories + Subcategories like a Tree menu:
Use plugin, called FoCal
4) view this topic
http://wpworks.wordpress.com/2011/01/13/displaying-categories-and-subcategories-tree-on-wordpress/

Categories