【问题标题】:Hiding category names on the frontend, but not backend, of WP site在 WP 网站的前端而不是后端隐藏类别名称
【发布时间】:2013-11-24 20:28:49
【问题描述】:

我正在使用以下代码,放置在我的 Wordpress 主题的 functions.php 中,从前端隐藏某些类别名称,这些名称仅用于组织帖子和填充滑块:

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', 10, 2);

问题是这段代码还在后端隐藏了这些相同的类别名称。复选框在那里,但类别的名称不存在。因此,我想修改此代码以允许管理员在后端查看隐藏在前端的类别的名称。帮助表示赞赏。 (ps,我对代码很陌生,所以请尽量描述性,或提出修改建议:非常感谢)

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    检查是否在函数开始时加载了管理环境,如果是,则通过返回原始列表终止执行:

    function the_category_filter( $thelist, $separator = '' )
    {
        if( is_admin() )
            return $thelist;
    
        // rest of the code
    }
    

    【讨论】:

    • 非常感谢。我不需要最后一个 }(它以 500 错误破坏了网站),但只需在顶部添加这两行就解决了后端的问题,同时保留了前端的功能
    猜你喜欢
    • 2019-11-28
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    相关资源
    最近更新 更多