admin管理员组文章数量:1130349
I have some code that displays the children categories on the screen but some parent categories do not have children if that is the case I don't want the section to be shown. does anybody know a way to do this? here is my current code to show the categories:
<div class="list-group products box">
<h4>Product Range</h4>
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories=get_categories(array( 'parent' => $category_id ));
$children = get_terms( 'product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
) );
foreach( $children as $subcat ){
?>
<a href="<?php echo get_term_link( $subcat->slug, 'product_cat' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php } } ?>
</div>
When a parent as no children it just shows the H4 Product Range
I have some code that displays the children categories on the screen but some parent categories do not have children if that is the case I don't want the section to be shown. does anybody know a way to do this? here is my current code to show the categories:
<div class="list-group products box">
<h4>Product Range</h4>
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories=get_categories(array( 'parent' => $category_id ));
$children = get_terms( 'product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
) );
foreach( $children as $subcat ){
?>
<a href="<?php echo get_term_link( $subcat->slug, 'product_cat' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php } } ?>
</div>
When a parent as no children it just shows the H4 Product Range
Share Improve this question edited Oct 24, 2018 at 9:31 Jenova1628 asked Oct 24, 2018 at 9:22 Jenova1628Jenova1628 332 silver badges9 bronze badges 2- means you do not want to show ` <h4>Product Range</h4>` if parent has no children? – Krishna Joshi Commented Oct 24, 2018 at 9:33
- bascially i want this section to be disabled so i cant see the div – Jenova1628 Commented Oct 24, 2018 at 14:06
1 Answer
Reset to default 2Try below code
<?php
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories = get_categories(array('parent' => $category_id));
$children = get_terms('product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
));
if (!empty($children)) {
echo ' <div class="list-group products box">
<h4>Product Range</h4>';
foreach ($children as $subcat) {
?>
<a href="<?php echo get_term_link($subcat->slug, 'product_cat'); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php
}
echo "</div>";
}
}
?>
Hope this helps
I have some code that displays the children categories on the screen but some parent categories do not have children if that is the case I don't want the section to be shown. does anybody know a way to do this? here is my current code to show the categories:
<div class="list-group products box">
<h4>Product Range</h4>
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories=get_categories(array( 'parent' => $category_id ));
$children = get_terms( 'product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
) );
foreach( $children as $subcat ){
?>
<a href="<?php echo get_term_link( $subcat->slug, 'product_cat' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php } } ?>
</div>
When a parent as no children it just shows the H4 Product Range
I have some code that displays the children categories on the screen but some parent categories do not have children if that is the case I don't want the section to be shown. does anybody know a way to do this? here is my current code to show the categories:
<div class="list-group products box">
<h4>Product Range</h4>
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories=get_categories(array( 'parent' => $category_id ));
$children = get_terms( 'product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
) );
foreach( $children as $subcat ){
?>
<a href="<?php echo get_term_link( $subcat->slug, 'product_cat' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php } } ?>
</div>
When a parent as no children it just shows the H4 Product Range
Share Improve this question edited Oct 24, 2018 at 9:31 Jenova1628 asked Oct 24, 2018 at 9:22 Jenova1628Jenova1628 332 silver badges9 bronze badges 2- means you do not want to show ` <h4>Product Range</h4>` if parent has no children? – Krishna Joshi Commented Oct 24, 2018 at 9:33
- bascially i want this section to be disabled so i cant see the div – Jenova1628 Commented Oct 24, 2018 at 14:06
1 Answer
Reset to default 2Try below code
<?php
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories = get_categories(array('parent' => $category_id));
$children = get_terms('product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
));
if (!empty($children)) {
echo ' <div class="list-group products box">
<h4>Product Range</h4>';
foreach ($children as $subcat) {
?>
<a href="<?php echo get_term_link($subcat->slug, 'product_cat'); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php
}
echo "</div>";
}
}
?>
Hope this helps
本文标签: phpDisable if there are no childrenWoocommerce
版权声明:本文标题:php - Disable if there are no children - Woocommerce 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749233857a2336948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论