get_the_category_rss

函式
get_the_category_rss ( $type = null )
引數
  • (string) $type Optional, default is the type returned by get_default_feed().
    Required:
    Default: null
返回值
  • (string) All of the post categories for displaying in the feed.
定義位置
相關方法
get_the_categorythe_category_rssget_the_category_listget_the_category_by_idget_category_rss_link
引入
2.1.0
棄用
-

get_the_category_rss: 這個函式檢索分配給當前文章的分類的格式化字串,以便在RSS饋送中使用。它不需要任何引數,並以字串形式返回格式化的類別列表。

檢索所有的文章類別,並將其格式化以用於feeds。

在feed迴圈中,當前文章的所有類別都將被檢索到,並新增了feed標記,因此它們可以很容易地被新增到RSS2、Atom、或RSS1和RSS0.91 RDF feeds。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 .= "tt<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 .= "tt<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 );
}
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 .= "tt<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 .= "tt<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 ); }
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 .= "tt<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 .= "tt<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 );
}

常見問題

FAQs
檢視更多 >