get_category_by_path()通过分类网址获取分类所有信息

目录

描述

译文

按含有类别缩略名的URL检索类别。

解析$category参数以获取类别缩略名。

搜索子路径并将其返回。如果$full_match设为false,找不到匹配路径时则返回符合缩略名的第一个类别。如果$full_match未设为false,返回空值。

出错时也可能返回WP_Error对象。使用该函数时注意检查对象。

原文

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.

get_category_by_path() 描述

用法

<?php get_category_by_path$category_path$full_match$output ?>

get_category_by_path() 用法

参数

$category_path

(string) (必填) URL containing category slugs.

默认值: None

$full_match

(boolean) (可选) Whether should match full path or not.

默认值: true

$output

(string) (可选) Constant OBJECT, ARRAY_A, or ARRAY_N

默认值: OBJECT

get_category_by_path() 参数

返回值

(null|object|array) 

Null on failure. Type is based on $output value.

get_category_by_path() 返回值

示例

Default usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
$categ = get_category_by_path('uncategorized');
echo "Category ".$categ->name;
?>

get_category_by_path() 示例

历史

添加于 版本: 2.1.0

get_category_by_path() 历史

源文件

get_category_by_path() 函数的代码位于 wp-includes/category.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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;
	}
}

get_category_by_path() 源文件