Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
get_the_categories - 过滤文章的类别数组 - WordPress钩子 - WordPress动力
当前浏览:首页 / WordPress钩子 / get_the_categories

apply_filters( 'get_the_categories', WP_Term[] $categories, int|false $post_id )

过滤文章的类别数组


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 3 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "a5b1222dbf5579dcde889eb3f6fe3a4a" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "d72d698f01562a7325d1d265501633b1" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "da3fa25e51008d111052e4f7cd1619a4" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

cat

category 分类more...

themore...


参数

$categories

(WP_Term[]) 要为文章返回的类别数组。

$post_id

(int|false) 文章 ID



源码

查看源码 官方文档


更新日志

版本描述
4.4.0添加了$post_id参数
3.1.0开始引入

使用示例

  • 示例1

    从特定文章的循环中删除某些类别。

    假设我们要删除ID为5的文章的类别,类别slug是cat-slug-a和cat-slug-b

    /**
     * Remove certain categories on post loop for a specific post
     * @param array $categories Array of categories
     * @return array $categories filtred categories
     */
    function wpdocs_remove_selected_categories( $categories ) {
    	if ( 5 == get_the_ID() ) { // Check if it is a specific post.
    		
    		$categories_to_remove = array(
    			'cat-slug-a',
    			'cat-slug-b'
    		); // Array of categories slug to be remove.
    
    		foreach ( $categories as $index => $single_cat ) {
    
    			if ( in_array( $single_cat->slug, $categories_to_remove ) ) {
    				unset( $categories[ $index ] ); // Remove the category.
    			}
    		}
    	}
    	
    	return $categories;
    }
    add_filter( 'get_the_categories', 'wpdocs_remove_selected_categories' );