返回介绍

get_category_by_path()

发布于 2017-09-10 22:57:33 字数 4186 浏览 1126 评论 0 收藏 0

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

Retrieve 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.


参数

$category_path

(string) (Required) URL containing category slugs.

$full_match

(bool) (Optional) Whether full path should be matched.

Default value: true

$output

(string) (Optional) 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 value: OBJECT


返回值

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


源代码

File: wp-includes/category.php

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;
	}
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: sanitize_title()
  • wp-includes/category.php: _make_cat_compat()
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/load.php: is_wp_error()

Used By

  • wp-includes/canonical.php: redirect_canonical()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文