admin管理员组

文章数量:1417064

We're sending RSS emails out to our list and would like to include the categories and tags.

But when we add them, due to the way WordPress delivers them, they just end up in one big long string/stacked end to end, one after the other, like this:

Digital ContentSocial MediaFacebookZuckerberg

I'm trying to find a way to get rid of the individual elements and just call my main, comma separated, category list, inside a single element, instead.

So instead of:

<category><![CDATA[Digital Content]]></category>
<category><![CDATA[Social Media]]></category>
<category><![CDATA[Facebook]]></category>
<category><![CDATA[Zuckerberg]]></category>

We'd have something like:

<category><![CDATA[Digital Content, Social Media, Facebook, Zuckerberg]]></category>

I'm looking at the_category_rss(), which seems designed for the purpose, but no matter what code I try I can't get it to override the individual s.

function rtt_rss_cats($the_list,$type) {
    $the_list = "PUT SOMETHING ELSE HERE";
    return $the_list;
}
add_filter('the_category_rss', 'rtt_rss_cats', 10, 1);

The main function appears to be get_the_category_rss(), and includes the filter. I just can't get it to work/override this:

function get_the_category_rss( $type = null ) {
        if ( empty( $type ) ) {
                $type = get_default_feed();
        }
        $categories = get_the_category();
        $tags       = get_the_tags();
        $the_list   = '';
        $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 );
           }
        }

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

        $cat_names = array_unique( $cat_names );
        foreach ( $cat_names as $cat_name ) {
                if ( 'rdf' == $type ) {
                        $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
                } elseif ( 'atom' == $type ) {
                        $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
                } else {
                        $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></category>\n";
                }
        }
        /**
         * Filters all of the post categories for display in a feed.
         *
         * @since 1.2.0
         *
         * @param string $the_list All of the RSS post categories.
         * @param string $type     Type of feed. Possible values include 'rss2', 'atom'.
         *                         Default 'rss2'.
         */
   return apply_filters( 'the_category_rss', $the_list, $type );
}

We're sending RSS emails out to our list and would like to include the categories and tags.

But when we add them, due to the way WordPress delivers them, they just end up in one big long string/stacked end to end, one after the other, like this:

Digital ContentSocial MediaFacebookZuckerberg

I'm trying to find a way to get rid of the individual elements and just call my main, comma separated, category list, inside a single element, instead.

So instead of:

<category><![CDATA[Digital Content]]></category>
<category><![CDATA[Social Media]]></category>
<category><![CDATA[Facebook]]></category>
<category><![CDATA[Zuckerberg]]></category>

We'd have something like:

<category><![CDATA[Digital Content, Social Media, Facebook, Zuckerberg]]></category>

I'm looking at the_category_rss(), which seems designed for the purpose, but no matter what code I try I can't get it to override the individual s.

function rtt_rss_cats($the_list,$type) {
    $the_list = "PUT SOMETHING ELSE HERE";
    return $the_list;
}
add_filter('the_category_rss', 'rtt_rss_cats', 10, 1);

The main function appears to be get_the_category_rss(), and includes the filter. I just can't get it to work/override this:

function get_the_category_rss( $type = null ) {
        if ( empty( $type ) ) {
                $type = get_default_feed();
        }
        $categories = get_the_category();
        $tags       = get_the_tags();
        $the_list   = '';
        $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 );
           }
        }

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

        $cat_names = array_unique( $cat_names );
        foreach ( $cat_names as $cat_name ) {
                if ( 'rdf' == $type ) {
                        $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
                } elseif ( 'atom' == $type ) {
                        $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
                } else {
                        $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></category>\n";
                }
        }
        /**
         * Filters all of the post categories for display in a feed.
         *
         * @since 1.2.0
         *
         * @param string $the_list All of the RSS post categories.
         * @param string $type     Type of feed. Possible values include 'rss2', 'atom'.
         *                         Default 'rss2'.
         */
   return apply_filters( 'the_category_rss', $the_list, $type );
}
Share Improve this question asked Aug 6, 2019 at 15:49 jongoesonjongoeson 635 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try to add the following code in functions.php of your theme and it should do the job.

function dcg_tidy_rss_categories( $the_list ) {
    $categories = get_the_category();
    $tags = get_the_tags();

    if ( ! empty( $categories ) ) {
        foreach ($categories as $category) {
            // remove the following line if you don't want Categories to appear
            $the_list = str_replace("<category><![CDATA[{$category->name}]]></category>", "$category->name, ", $the_list);
        }
    }

    if ( ! empty( $tags ) ) {
        foreach ($tags as $tag) {
            // remove the following line if you don't want Tags to appear
            $the_list = str_replace("<category><![CDATA[{$tag->name}]]></category>", "$tag->name, ", $the_list);
        }
    }

    return "<category><![CDATA[" . preg_replace('/\s+/S', " ", trim( rtrim( $the_list, ", \t\n" ) ) ) . "]]></category>";
}
add_filter( 'the_category_rss', 'dcg_tidy_rss_categories' );

本文标签: categoriesHow replace individual ltcategorygt elements in the RSS feed with a single ltcategorygt string