��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>How to not treat categories as tags in feeds
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to not treat categories as tags in feeds

programmeradmin5浏览0评论

Basically we're (ab)using Category to determine whether a post is a News post or a Blog post, and so it's not a category in the traditional sense to the outside world. I'd like the category to not be indexed by feed-readers/crawlers, only the tags.

I found an easy way to do it with an easy change to 1 function in the core, but I'd rather avoid that if possible.

Is there a way to do that without modifying core? (Specifically get_the_category_rss in wp-includes/feed.php)

EDIT: What I mean specifically is to not list the category in the feed. For example, this is what part the current RSS2 feed looks like:

<category><![CDATA[blog]]></category>
<category><![CDATA[test]]></category>
<category><![CDATA[testing]]></category>
<category><![CDATA[yeah!]]></category>

"blog" is a Wordpress Category, while "test", "testing", and "yeah!" are Wordpress Tags.

What I want is to exclude the entry <category><![CDATA[blog]]></category> entirely from the feed. The same should apply to Atom feeds and any others.

Basically we're (ab)using Category to determine whether a post is a News post or a Blog post, and so it's not a category in the traditional sense to the outside world. I'd like the category to not be indexed by feed-readers/crawlers, only the tags.

I found an easy way to do it with an easy change to 1 function in the core, but I'd rather avoid that if possible.

Is there a way to do that without modifying core? (Specifically get_the_category_rss in wp-includes/feed.php)

EDIT: What I mean specifically is to not list the category in the feed. For example, this is what part the current RSS2 feed looks like:

<category><![CDATA[blog]]></category>
<category><![CDATA[test]]></category>
<category><![CDATA[testing]]></category>
<category><![CDATA[yeah!]]></category>

"blog" is a Wordpress Category, while "test", "testing", and "yeah!" are Wordpress Tags.

What I want is to exclude the entry <category><![CDATA[blog]]></category> entirely from the feed. The same should apply to Atom feeds and any others.

Share Improve this question edited Dec 16, 2010 at 20:46 David Ly asked Dec 16, 2010 at 19:55 David LyDavid Ly 1858 bronze badges 4
  • Do you want to prevent indexing of category feed by services only or kill this part of functionality completely? – Rarst Commented Dec 16, 2010 at 20:14
  • Well, as far as I can tell in the feed output it looks like it's treating the category the same way it's treating tags. Basically in the feed I don't want the category to be outputted. Category in our case doesn't carry the same semantic meaning as normal categories. – David Ly Commented Dec 16, 2010 at 20:23
  • Yes, I understand. I ask what exactly do you want to happen - category links to not being displayed on front-end, category feeds contain no items, category feeds result in 404 error, something else? – Rarst Commented Dec 16, 2010 at 20:38
  • Tried to clarify what I meant in the edit. Let me know if that's not clear enough. – David Ly Commented Dec 16, 2010 at 20:46
Add a comment  | 

3 Answers 3

Reset to default 4

Tricky. It lumps categories and tags together pretty good. Had couple of approaches here is least messy:

add_filter('the_category_rss', 'remove_rss_categories');

function remove_rss_categories( $the_list ) {

    $categories = get_the_category();

    foreach ($categories as $category)
        $the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", '', $the_list);

    return $the_list;
}

@Rarst got me on the right track. I modified it to work on all the different types of feed with some copy/paste/modify from the exact code used in the WP core to make sure escaping is done in the same way etc.

Please give @Rarst the +1's, as it's really his solution. Just posting this as a more complete solution. I added this to my theme's function.php file and it did the trick.

NOTE that this could break in future if WP changes the format of the feeds. This is working as of WP 3.0

add_filter('the_category_rss', 'remove_rss_categories', 10, 2); 

function remove_rss_categories( $the_list, $type ) {
    if ( empty($type) )
      $type = get_default_feed();

    $categories = get_the_category();    
    $cat_names = array();

    $filter = 'rss';
    if ( 'atom' == $type )
      $filter = 'raw';

    if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
      $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
    }
    $cat_names = array_unique($cat_names);

    foreach ($cat_names as $cat_name) {

      if ( 'rdf' == $type )
        $the_list .= str_replace("\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n", '', $the_list);
      elseif ( 'atom' == $type )
        $the_list .= str_replace(sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( 'url' ) ) ), esc_attr( $cat_name ) ), '', $the_list);
      else
        $the_list = str_replace("\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n", '', $the_list);
    }
    return $the_list;
}

I made category removal with such code in functions.php:

add_filter( 'the_category_rss', 'remove_category_from_rss', 10, 2 );
function remove_category_from_rss( $the_list, $type ){
    return;
}
发布评论

评论列表(0)

  1. 暂无评论