如何将RSS提要中的单个<ategory>元素替换为单个<ategory>字符串

时间:2019-08-06 作者:jongoeson

我们正在向我们的列表发送RSS电子邮件,希望包括类别和标签。

但是,当我们添加它们时,由于WordPress传递它们的方式,它们只会以一个长长的字符串/端到端的堆叠方式结束,一个接一个,如下所示:

数字内容社交媒体Facebook扎克伯格

我试图找到一种方法来摆脱单个元素,只需在单个元素中调用main、逗号分隔的类别列表即可。

因此,不是:

<category><![CDATA[Digital Content]]></category>
<category><![CDATA[Social Media]]></category>
<category><![CDATA[Facebook]]></category>
<category><![CDATA[Zuckerberg]]></category>
我们会有这样的东西:

<category><![CDATA[Digital Content, Social Media, Facebook, Zuckerberg]]></category>
我正在查看\\u category\\u rss(),它似乎是为这个目的而设计的,但无论我尝试什么代码,我都无法让它覆盖单个的。

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);
主要函数似乎是get\\u The\\u category\\u rss(),并包含过滤器。我就是不能让它工作/覆盖这个:

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

1 个回复
最合适的回答,由SO网友:Dipak G. 整理而成

尝试在函数中添加以下代码。php是你的主题,它应该可以完成这项工作。

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\' );

相关推荐

Get post categories

我想根据帖子类别做一个过滤器。我试图在数据类别属性中添加类别名称,但似乎不起作用。这就是我所尝试的:<article id=\"post-<?php the_ID(); ?>\" <?php post_class(); ?> data-category=\"<?php $category_detail=get_the_category(); foreach($category_detail as $cd){ $cd->cat_name;}?>\">&#