WPSEO_Replace_Vars::retrieve_category_title()privateYoast 1.0

Retrieve the current or first category title. To be used for import data from AIOSEO. The code derives from AIOSEO's way of dealing with that var, so we can ensure 100% seamless transition.

{} Это метод класса: WPSEO_Replace_Vars{}

Хуков нет.

Возвращает

Строку|null.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->retrieve_category_title();

Код WPSEO_Replace_Vars::retrieve_category_title() Yoast 19.6.1

private function retrieve_category_title() {
	if ( empty( $this->args ) || empty( $this->args->ID ) ) {
		return null;
	}
	$post_id = $this->args->ID;

	$post       = get_post( $post_id );
	$taxonomies = get_object_taxonomies( $post, 'objects' );

	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
		if ( ! $taxonomy->hierarchical ) {
			continue;
		}
		$post_terms = get_the_terms( $post_id, $taxonomy_slug );
		if ( is_array( $post_terms ) && count( $post_terms ) > 0 ) {
			// AiOSEO takes the name of whatever the first hierarchical taxonomy is.
			$term = $post_terms[0];
			if ( $term ) {
				return $term->name;
			}
		}
	}

	return null;
}