返回介绍

get_page_by_path()

发布于 2017-09-10 23:42:04 字数 6083 浏览 1335 评论 0 收藏 0

get_page_by_path( string $page_path,  string $output = OBJECT,  string|array $post_type = 'page' )

Retrieves a page given its path.


description


参数

$page_path

(string) (Required) Page path.

$output

(string) (Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$post_type

(string|array) (Optional) Post type or array of post types.

Default value: 'page'


返回值

(WP_Post|array|null) WP_Post (or array) on success, or null on failure.


源代码

File: wp-includes/post.php

function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
	global $wpdb;

	$last_changed = wp_cache_get_last_changed( 'posts' );

	$hash = md5( $page_path . serialize( $post_type ) );
	$cache_key = "get_page_by_path:$hash:$last_changed";
	$cached = wp_cache_get( $cache_key, 'posts' );
	if ( false !== $cached ) {
		// Special case: '0' is a bad `$page_path`.
		if ( '0' === $cached || 0 === $cached ) {
			return;
		} else {
			return get_post( $cached, $output );
		}
	}

	$page_path = rawurlencode(urldecode($page_path));
	$page_path = str_replace('%2F', '/', $page_path);
	$page_path = str_replace('%20', ' ', $page_path);
	$parts = explode( '/', trim( $page_path, '/' ) );
	$parts = esc_sql( $parts );
	$parts = array_map( 'sanitize_title_for_query', $parts );

	$in_string = "'" . implode( "','", $parts ) . "'";

	if ( is_array( $post_type ) ) {
		$post_types = $post_type;
	} else {
		$post_types = array( $post_type, 'attachment' );
	}

	$post_types = esc_sql( $post_types );
	$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
	$sql = "
		SELECT ID, post_name, post_parent, post_type
		FROM $wpdb->posts
		WHERE post_name IN ($in_string)
		AND post_type IN ($post_type_in_string)
	";

	$pages = $wpdb->get_results( $sql, OBJECT_K );

	$revparts = array_reverse( $parts );

	$foundid = 0;
	foreach ( (array) $pages as $page ) {
		if ( $page->post_name == $revparts[0] ) {
			$count = 0;
			$p = $page;

			/*
			 * Loop through the given path parts from right to left,
			 * ensuring each matches the post ancestry.
			 */
			while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {
				$count++;
				$parent = $pages[ $p->post_parent ];
				if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] )
					break;
				$p = $parent;
			}

			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
				$foundid = $page->ID;
				if ( $page->post_type == $post_type )
					break;
			}
		}
	}

	// We cache misses as well as hits.
	wp_cache_set( $cache_key, $foundid, 'posts' );

	if ( $foundid ) {
		return get_post( $foundid, $output );
	}
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_cache_get_last_changed()
  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/formatting.php: esc_sql()
  • wp-includes/post.php: get_post()
  • wp-includes/wp-db.php: wpdb::get_results()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/embed.php: get_post_embed_url()
  • wp-includes/rewrite.php: wp_resolve_numeric_slug_conflicts()
  • wp-admin/includes/upgrade.php: wp_install_maybe_enable_pretty_permalinks()
  • wp-includes/class-wp.php: WP::parse_request()
  • wp-includes/class-wp-query.php: WP_Query::is_page()
  • wp-includes/class-wp-query.php: WP_Query::is_single()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/class-wp-query.php: WP_Query::parse_query()
  • wp-includes/rewrite.php: url_to_postid()
  • Show 4 more used by Hide more used by

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

    Page Path
    This is the equivalent of the pagename query, as in: index.php?pagename=parent-page/sub-page.

    Code for the above could be written as (assuming parent-page/sub-page is actually the path to a page):

    
    get_page_by_path('parent-page/sub-page');
    

    For non-hierarchical custom post types, you need to use just the slug in tandem with the post_type parameter.

    
    //Returns nothing, assumes animals is the rewrite slug for the animal CPT
    get_page_by_path('animals/cat', OBJECT, 'animal');
    
    //Returns the animal with the slug 'cat'
    get_page_by_path('cat', OBJECT, 'animal');
    

    The functions basename() and untrailingslashit() are handy for grabbing the last part of the URL for this:

    
    $page_path = 'animals/cat/';
    get_page_by_path( basename( untrailingslashit( $page_path ) ) , OBJECT, 'animal');
    

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

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

发布评论

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