It’s for a custom categories widget. There are 2 basic conditions I’m testing for: a parent category page or a child category page. If it’s a child category page then there are 2 further conditions I need to check. If condition 1 then display the widget, if condition 2, don’t display the widget (i.e, there are no products to display). The final else statement is for parent category pages.
Here’s the code:
<?php
if (is_category( )) {
$thiscat = get_category( get_query_var( 'cat' ) );
$catid = $thiscat->cat_ID;
$parent = $thiscat->category_parent;
$countchildren = count (get_term_children( $parent, category ));
if (!empty ($parent) && $countchildren >=2 ) {
//child category pages
$catlist = get_categories(
array(
'child_of' => $parent,
'orderby' => 'id',
'order' => 'DESC',
'exclude' => $catid,
'hide_empty' => '0'
) );
//widget title
echo '<h2 class="widget-title"><span>' . __( 'Related Products', 'codilight-lite' ) . '</span></h2>';
echo '<ul>';
//categories list
foreach ( $catlist as $category ) {
echo '<li><h3 class="entry-title"><a href="' . get_category_link( $category->cat_ID ) . '" rel="bookmark">' . $category->cat_name . '</a></h3></li>';
}
echo '</ul>';
}
elseif (!empty ($parent) && $countchildren <2 ) {
echo '<h2 class="widget-title"><span>' . __( 'Related Products', 'codilight-lite' ) . '</span></h2>';
echo '<p>' . __( 'more products coming soon', 'codilight-lite' ) . '</p>';
}
else {
//parent category pages
$catname = get_cat_name( $catid );
$catlist = get_categories(
array(
'child_of' => $catid,
'orderby' => 'id',
'order' => 'DESC',
'hide_empty' => '0'
) );
//widget title
echo '<h2 class="widget-title"><span>' . $catname . __( ' Products', 'codilight-lite') . '</span></h2>';
echo '<ul>';
foreach ( $catlist as $category ) {
echo '<li><h3 class="entry-title"><a href="' . get_category_link( $category->cat_ID ) . '" rel="bookmark">' . $category->cat_name . '</a></h3></li>';
}
echo '</ul>';
}
}
?>
I realize now I’m going about it the wrong way and I could either do what I’ve already done in the code above, i.e, display the widget with the text, “more products coming soon” or remove the elseif
statement and change the final else
statement to elseif ( empty ($parent) )
.
-
This reply was modified 8 years, 2 months ago by jrcollins.