admin管理员组

文章数量:1323035

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;
}

本文标签: How to not treat categories as tags in feeds