Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_list_categories unlink or style empty WordPress

  • SOLVED

Sounds simple enough but I cant seem to figure it out...

I need to re-style any empty categories/terms. So terms without a post will appear in black and terms with a post will be white.


<?php // New Timeline functioning from custom date taxonomies
$taxonomy = 'shows';
$orderby = 'slug';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$hide_empty = 0;

$catsy = get_the_category();
$myCat = $catsy[0]->cat_ID;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $hide_empty,
'current_category' => $myCat
); ?>

<div class="years">
<ul class="shows-dates">
<?php wp_list_categories( $args ); ?>
</ul>
</div>

Answers (1)

2013-07-18

Arnav Joy answers:

change function as follows:-



wp_list_categories( array( 'walker' => new WPQuestions_Walker ) );


write this in functions.php



<?php

class WPQuestions_Walker extends Walker_Category {

function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {

extract($args);



$cat_name = esc_attr( $category->name );

$cat_name = apply_filters( 'list_cats', $cat_name, $category );



// ---

$termchildren = get_term_children( $category->term_id, $category->taxonomy );

if($category->count >0 ){

$aclass = ' class="cat_has_posts" ';

}
else
$aclass = ' class="cat_has_no_posts" ';



$link = '<a '.$aclass.' href="' . esc_url( get_term_link($category) ) . '" ';

// ---



if ( $use_desc_for_title == 0 || empty($category->description) )

$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';

else

$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';

$link .= '>';

$link .= $cat_name . '</a>';



if ( !empty($show_count) )

$link .= ' (' . intval($category->count) . ')';



if ( 'list' == $args['style'] ) {

$output .= "\t<li";

$class = 'cat-item cat-item-' . $category->term_id;



if ( !empty($current_category) ) {

$_current_category = get_term( $current_category, $category->taxonomy );

if ( $category->term_id == $current_category )

$class .= ' current-cat';

elseif ( $category->term_id == $_current_category->parent )

$class .= ' current-cat-parent';

}

$output .= ' class="' . $class . '"';

$output .= ">$link\n";

} else {

$output .= "\t$link<br />\n";

}

}

}
?>



now the li has "cat_has_posts" and "cat_has_no_posts" classes


leannekera comments:

Looks to be working great for categories. Anyway I can attach this to my taxonomie 'shows' ?


leannekera comments:

Got it working, thank you so much again Arnav

Final Code:

<!--<div id="smart-archives-fancy" >-->
<?php // New Timeline functioning from custom date taxonomies
$taxonomy = 'shows';
$orderby = 'slug';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$hide_empty = 0;

$catsy = get_the_category();
$myCat = $catsy[0]->cat_ID;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $hide_empty,
'current_category' => $myCat,
'walker' => new WPQuestions_Walker,
); ?>

<div class="years">
<ul class="shows-dates">
<?php //wp_list_categories( $args );
wp_list_categories( $args );
?>
</ul>
</div>


leannekera comments:

Hi Arnav,

Ive just noticed this has removed my previous styles that highlighted the parent category and current. Can you help?