函数文档

get_category_by_path()

💡 云策文档标注

概述

get_category_by_path() 函数用于根据包含分类别名的 URL 路径检索分类。它解析路径参数,尝试匹配完整路径或返回第一个匹配的分类,并支持多种返回类型。

关键要点

  • 函数基于 URL 路径中的分类别名来查找分类,支持路径解析和匹配逻辑。
  • 参数 $full_match 控制是否要求完整路径匹配,默认为 true;$output 指定返回类型,如 OBJECT、ARRAY_A 或 ARRAY_N。
  • 返回类型可以是 WP_Term 对象、数组、WP_Error 或 null,取决于 $output 值和函数执行情况。
  • 函数内部使用 get_terms() 和 get_term() 进行查询,并调用 _make_cat_compat() 确保兼容性。
  • 使用时需检查返回值是否为 WP_Error,以处理可能的错误情况。

代码示例

$categ = get_category_by_path( 'uncategorized' );
printf( __( 'Category %s', 'textdomain' ), $categ->name );

📄 原文内容

Retrieves a category based on URL containing the category slug.

Description

Breaks the $category_path parameter up to get the category slug.

Tries to find the child path and will return it. If it doesn’t find a match, then it will return the first category matching slug, if $full_match, is set to false. If it does not, then it will return null.

It is also possible that it will return a WP_Error object on failure. Check for it when using this function.

Parameters

$category_pathstringrequired
URL containing category slugs.
$full_matchbooloptional
Whether full path should be matched.

Default:true

$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively.

Default:OBJECT

Return

WP_Term|array|WP_Error|null Type is based on $output value.

Source

function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
	$category_path  = rawurlencode( urldecode( $category_path ) );
	$category_path  = str_replace( '%2F', '/', $category_path );
	$category_path  = str_replace( '%20', ' ', $category_path );
	$category_paths = '/' . trim( $category_path, '/' );
	$leaf_path      = sanitize_title( basename( $category_paths ) );
	$category_paths = explode( '/', $category_paths );
	$full_path      = '';

	foreach ( (array) $category_paths as $pathdir ) {
		$full_path .= ( '' !== $pathdir ? '/' : '' ) . sanitize_title( $pathdir );
	}

	$categories = get_terms(
		array(
			'taxonomy' => 'category',
			'get'      => 'all',
			'slug'     => $leaf_path,
		)
	);

	if ( empty( $categories ) ) {
		return;
	}

	foreach ( $categories as $category ) {
		$path        = '/' . $leaf_path;
		$curcategory = $category;

		while ( ( 0 !== $curcategory->parent ) && ( $curcategory->parent !== $curcategory->term_id ) ) {
			$curcategory = get_term( $curcategory->parent, 'category' );

			if ( is_wp_error( $curcategory ) ) {
				return $curcategory;
			}

			$path = '/' . $curcategory->slug . $path;
		}

		if ( $path === $full_path ) {
			$category = get_term( $category->term_id, 'category', $output );
			_make_cat_compat( $category );

			return $category;
		}
	}

	// If full matching is not required, return the first cat that matches the leaf.
	if ( ! $full_match ) {
		$category = get_term( reset( $categories )->term_id, 'category', $output );
		_make_cat_compat( $category );

		return $category;
	}
}

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes