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/
Related
i want to display multiple categories with their post counts with help of short code without plugin.
i found many functions code here but didn't work with multiple categories with shortcode.
how to create function that is use to display all categories post counts using shortcode.
Example:
category 1: 20 posts
category 2: 10 posts
category 3: 15 posts
the display results 20,10 or 15 should be shown by help of shortcode.
This is a basic way to show a list of categories with their post counts. You could use other objects to link to the category pages etc. From what you described you just need a list, but this is the foundation.
function category_post_count() {
ob_start(); //php output buffer
$categories = get_categories(); ?>
<ul>
<?php foreach($categories as $cat) { ?>
<li><?php echo $cat->name . ': ' . $cat->count; . ' posts '; ?></li>
<?php } ?>
</ul>
<?php $endBuffer = ob_get_clean();
return $endBuffer;
}
add_shortcode( 'category_post_count', 'category_post_count' );
Then all you need to do is add this to the area where you want it to show up on the page...
[category_post_count]
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.
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 "       ";
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.
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']'");
}
?>
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.