get_the_categories

最后更新于:2021-11-26 23:46:09

apply_filters( ‘get_the_categories’, GC_Term[] $categories, int|false $post_id )

Filters the array of categories to return for a post.

参数

$categories

(GC_Term[])
An array of categories to return for the post.

$post_id

(int|false)
ID of the post.

源文件

文件: gc-includes/category-template.php

View on Trac

/**
 * Remove certain categories on post loop for a specific post
 * @param array $categories Array of categories
 * @return array $categories filtred categories
 */
function gcdocs_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', 'gcdocs_remove_selected_categories' );