admin管理员组文章数量:1313290
I'm using the following code, placed in the functions.php of my theme, to hide certain categories used to organise posts and populate sliders from the posts themselves and their category listings:
function the_category_filter($thelist,$separator=' ') {
// list the IDs of the categories to exclude
$exclude = array(1,32,42,4);
// 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', 1, 32, 42, 4);
This works well, but it also hides the categories in the post edit backend. I can see the tick box, but the name of the category is hidden.
Can someone suggest a tweak to the above code that preserves its function of hiding categories on the frontend, but keeps categories visible in the post edit window / the WP backend?
I'm using the following code, placed in the functions.php of my theme, to hide certain categories used to organise posts and populate sliders from the posts themselves and their category listings:
function the_category_filter($thelist,$separator=' ') {
// list the IDs of the categories to exclude
$exclude = array(1,32,42,4);
// 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', 1, 32, 42, 4);
This works well, but it also hides the categories in the post edit backend. I can see the tick box, but the name of the category is hidden.
Can someone suggest a tweak to the above code that preserves its function of hiding categories on the frontend, but keeps categories visible in the post edit window / the WP backend?
Share Improve this question asked Nov 22, 2013 at 23:42 Ian DouglasIan Douglas 12 bronze badges2 Answers
Reset to default 1Just don't apply the filter if is_admin()
.
Unrelated- you should also take a look at add_filter
, your arguments are not correct.
You can do something like:
if ( ! is_admin() ) {
// Exclude categories.
}
More examples in: https://developer.wordpress/reference/functions/is_admin/
本文标签: Hide categories from frontendbut not adminpost editor
版权声明:本文标题:Hide categories from frontend, but not adminpost editor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741911198a2404455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论