我想在WordPress帖子中包含类似于\\u category函数的功能,默认情况下,它会回显一个链接列表,指向帖子所附加的所有类别,这些类别之间用逗号分隔,但我想在某些视图中排除某些类别。在某些情况下,我必须排除所有儿童的出现,在其他情况下,我必须排除顶级类别或随机特定类别。
从单个帖子页面的_CATEGORY中排除类别
2 个回复
SO网友:Bainternet
不确定,但我想你可以试试这个
function my_category_filter($cats){
//exclude these from displaying
$exclude = array("Large Announcement", "Announcement", "News");
$Catds = array();
foreach ($cats as$cat){
if (!in_array($cat,$exclude)){$Catsa[] = $cat;}
}
return $Catsa;
}
add_filter(\'the_category\',\'my_category_filter\');
SO网友:steffanjj
从WordPress支持论坛输入这个方便的功能-只需将它添加到主题的功能中即可。php文件:
function the_category_filter($thelist,$separator=\' \') {
// list the IDs of the categories to exclude
$exclude = array(366);
// create an empty array
$exclude2 = array();
// loop through the excluded IDs and get their actual names
foreach($exclude as $c) {
// store the names in the second array
$exclude2[] = get_cat_name($c);
}
// get the list of categories for the current post
$cats = explode($separator,$thelist);
// create another empty array
$newlist = array();
foreach($cats as $cat) {
// remove the tags from each category
$catname = trim(strip_tags($cat));
// check against the excluded categories
if(!in_array($catname,$exclude2))
// if not in that list, add to the new array
$newlist[] = $cat;
}
// return the new, shortened list
return implode($separator,$newlist);
}
// add the filter to \'the_category\' tag
add_filter(\'the_category\',\'the_category_filter\', 10, 2);
最初,我确实对这个函数有一些问题;经过一番修补,我意识到我需要在\\u category()标记中声明一个分隔符,如逗号、破折号等。在本例中,我只使用了一个空格-the_category(\' \')
— 这不起作用,但将其换成一个不间断的空间就成功了:the_category(\' \').
信用证:http://www.stemlegal.com/greenhouse/2012/excluding-categories-from-the-category-tag-in-wordpres/
结束