1. 首页
  2. WordPress 函数手册

get_category_by_path()

get_category_by_path( string $category_path, bool $full_match = true, string $output = OBJECT )

基于包含类别slug的URL检索类别。
Retrieve category based on URL containing the category slug.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

断开$category_path参数以获取category slug。

试图找到子路径并将其返回。如果没有找到匹配项,那么它将返回第一个类别匹配的slug,如果$full_match设置为false。如果没有,则返回空值。

它也可能在失败时返回WP_Error对象。使用此功能时请检查。


参数(Parameters)

参数 类型 必填 说明
$category_path (string) 必需 包含类别段塞的URL。
$full_match (bool) 可选 是否应匹配完整路径。
$output (string) 可选 所需的返回类型。数组或数组中的一个,分别对应于WP术语对象、关联数组或数值数组。

返回(Return)

(WP|Term|array|WP|Error|null)类型基于$output value。


源码(Source)

/**
 * Retrieve category based on URL containing the category slug.
 *
 * 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.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool $full_match Optional. Whether full path should be matched.
 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
 * @return object|array|WP_Error|void Type is based on $output value.
 */
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( 'category', array('get' => 'all', 'slug' => $leaf_path) );

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

	foreach ( $categories as $category ) {
		$path = '/' . $leaf_path;
		$curcategory = $category;
		while ( ( $curcategory->parent != 0 ) && ( $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;
	}
}
更新版本 源码位置 使用 被使用
2.1.0 wp-includes/category.php:121 1 function 5

笔记(Notes)

基本示例

get_category_by_path() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_category_by_path/

发表评论

您的电子邮箱地址不会被公开。