admin管理员组文章数量:1404347
This is probably a confusing title of my question, but it's exactly what's describing my problem best.
I want to add the category-slug as classname to my wp_list_categories()
output.
I found a really simple function that does exactly that it works perfectly.
add_filter('wp_list_categories', 'add_slug_css_list_categories');
function add_slug_css_list_categories($list) {
$cats = get_categories();
foreach($cats as $cat) {
$find = 'cat-item-' . $cat->term_id . '"';
$replace = 'category-' . $cat->slug . '"';
$list = str_replace( $find, $replace, $list );
$find = 'cat-item-' . $cat->term_id . ' ';
$replace = 'category-' . $cat->slug . ' ';
$list = str_replace( $find, $replace, $list );*/
}
return $list;
}
So now I have class-categoryslug
in my li
s for wp_list_categories()
I have just one more little tweak to add to it.
I wrote a function to use wp_list_categories()
also to list my taxonomy terms for a hierarchical taxonomy and a custom-post-type … looks like this.
function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
$show_count = 0;
$pad_counts = 0;
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
return wp_list_categories( $args );
}
So I can use wr_list_taxonomy()
and all my categories are listed.
I want to have the same thing for my taxonomy terms as well, so that the classnames have the slug of the taxonomy term associated with it.
This would be easy because I only have to replace $cats = get_categories();
with $cats = get_terms('event_type');
…
However I can only do either or. So either I choose to use $cats = get_categories();
and all my normal categories for the normal blog posts have the category-slug as classname or I use $cats = get_terms('event_type');
and all my taxonomy terms have the term-slug as classname.
I have no idea, how I can determine inside the function add_slug_css_list_categories()
if the function is currently fired for normal categories or for my tax-terms.
I thought of
add_filter('wp_list_categories', 'add_slug_css_list_categories');
function add_slug_css_list_categories($list) {
//$cats = get_terms('event_type');
$cats = get_categories();
//if ( empty( $cats ) )
// $cats = get_categories();
But that doesn't work. Any ideas?
This is probably a confusing title of my question, but it's exactly what's describing my problem best.
I want to add the category-slug as classname to my wp_list_categories()
output.
I found a really simple function that does exactly that it works perfectly.
add_filter('wp_list_categories', 'add_slug_css_list_categories');
function add_slug_css_list_categories($list) {
$cats = get_categories();
foreach($cats as $cat) {
$find = 'cat-item-' . $cat->term_id . '"';
$replace = 'category-' . $cat->slug . '"';
$list = str_replace( $find, $replace, $list );
$find = 'cat-item-' . $cat->term_id . ' ';
$replace = 'category-' . $cat->slug . ' ';
$list = str_replace( $find, $replace, $list );*/
}
return $list;
}
So now I have class-categoryslug
in my li
s for wp_list_categories()
I have just one more little tweak to add to it.
I wrote a function to use wp_list_categories()
also to list my taxonomy terms for a hierarchical taxonomy and a custom-post-type … looks like this.
function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
$show_count = 0;
$pad_counts = 0;
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
return wp_list_categories( $args );
}
So I can use wr_list_taxonomy()
and all my categories are listed.
I want to have the same thing for my taxonomy terms as well, so that the classnames have the slug of the taxonomy term associated with it.
This would be easy because I only have to replace $cats = get_categories();
with $cats = get_terms('event_type');
…
However I can only do either or. So either I choose to use $cats = get_categories();
and all my normal categories for the normal blog posts have the category-slug as classname or I use $cats = get_terms('event_type');
and all my taxonomy terms have the term-slug as classname.
I have no idea, how I can determine inside the function add_slug_css_list_categories()
if the function is currently fired for normal categories or for my tax-terms.
I thought of
add_filter('wp_list_categories', 'add_slug_css_list_categories');
function add_slug_css_list_categories($list) {
//$cats = get_terms('event_type');
$cats = get_categories();
//if ( empty( $cats ) )
// $cats = get_categories();
But that doesn't work. Any ideas?
Share Improve this question edited Jan 30, 2020 at 9:20 Viktor Borítás 3042 silver badges11 bronze badges asked Apr 20, 2012 at 8:10 mathiregistermathiregister 1,54313 gold badges55 silver badges78 bronze badges1 Answer
Reset to default 2Wordpress do:
apply_filters( 'wp_list_categories', $output, $args );
You can do:
function add_slug_css_list_categories($list,$args) { }
Then you can use $args to determine what kind of list you have.
Not sure if you must do that but when you add the filter there is also a variable for accepted arguments:
add_filter('wp_list_categories', 'add_slug_css_list_categories',10,2);
本文标签: Add filter to wplistcategories and query what type of taxonomyterms it use
版权声明:本文标题:Add filter to wp_list_categories and query what type of taxonomy-terms it use? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744790414a2625252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论