Reputation: 1991
I have this category on my wordpress:
Test1
- Sub1OfTest1
- Sub2OfTest1
Test2
- Sub1OfTest2
- Sub2OfTest2
Now iam at url: http://localhost/wordpress/category/test1
I write the following code on file category-test1.php
<?php
$categories = get_categories('child_of=2');
print_r($categories);
foreach ($categories as $category) : ?>
<div class="qitem">
<a href="<?php get_category_link( $category->term_id ); ?>" title="<?php echo $category->name; ?>">
<?php echo $category->name; ?>
</a>
<h4>
<span class="caption">
<?php echo $category->name; ?>
</span>
</h4>
<p>
<span class="caption">
<?php echo $category->description; ?>
</span>
</p>
</div>
<?php
endforeach;
?>
I'm trying to show sub category of Test1 but the code only return array(). What did I miss?
Upvotes: 5
Views: 24307
Reputation: 1
//This is coding of getting sub category and sub-sub category
$args = array
(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => false,
'include' => array(11,281,28,51,99,93,33,55,107,20),
'exclude' => array(32,29),
);
$product_categories = get_terms( 'product_cat', $args );
// echo '<pre>';
// print_r($product_categories);
// echo '</pre>';
foreach($product_categories as $data):
if($data->slug = 'cooking')
{
$child_arg = array('hide_empty'=>false,'parent'=>$data->term_id,'exclude'=>array(32,29));
}
else
{
$child_arg = array('hide_empty'=>false,'parent'=>$data->term_id);
}
$child_terms = get_terms('product_cat',$child_arg);
// echo "<pre>";
// print_r($child_terms);
// echo "</pre>";
foreach($child_terms as $data1):
if($data1->slug = 'cooking')
{
$sub_child_arg = array('hide_empty'=>false,'parent'=>$data1->term_id,'exclude'=>array(32,29));
}
else
{
$sub_child_arg = array('hide_empty'=>false,'parent'=>$data1->term_id);
}
$sub_child_terms = get_terms('product_cat',$sub_child_arg);
// echo "<pre>";
// print_r($sub_child_terms);
// echo "</pre>";
endforeach;
endforeach;
?>
Upvotes: 0
Reputation: 56391
try this too - Show wordpress subcategories only
that topic might help too, as there are different solutions for showing subcategories.
Upvotes: 0
Reputation: 1208
Is that category empty? By default WordPress hides emptr categories. try:
$categories = get_categories('child_of=2&hide_empty=0');
Edit: Fixed, thank you @Stoep
Upvotes: 11
Reputation: 28795
The child_of
argument of get_categories
specifies the category by it's ID - assuming you created your categories in order, the code get_categories('child_of=2')
is probably requesting the subcategories of Sub1OfTest1.
To get the ID of a category, go to Posts → Categories and click on a category. The cat_ID will be in the url of the page.
Upvotes: 0