get_page_by_path()通过页面的路径获取页面信息

目录

描述

Retrieves a page given its path.

get_page_by_path() 描述

用法

<?php get_page_by_path$page_path$output$post_type ); ?>

get_page_by_path() 用法

参数

$page_path

(string) (必填) Page path.

默认值: None

$output

(string) (可选) Output type. OBJECT, ARRAY_N, or ARRAY_A.

默认值: OBJECT

$post_type

(string or array) (可选) Post type or array of post types.

默认值: 'page'

get_page_by_path() 参数

返回值

(WP_Post|null) 

WP_Post on success or null on failure.

get_page_by_path() 返回值

示例

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):

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
get_page_by_path('parent-page/sub-page');

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

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
$page_path = 'animals/cat/';
get_page_by_path( basename( untrailingslashit( $page_path ) ) , OBJECT, 'animal');

get_page_by_path() 示例

注意

  • 使用到 global: (object) $wpdb
  • Returns false for "private" pages/posts

get_page_by_path() 注意

历史

添加于 版本: 2.1.0

get_page_by_path() 历史

源文件

get_page_by_path() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieves a page given its path.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_path Page path.
 * @param string       $output    Optional. Output type. Accepts OBJECT, ARRAY_N, or ARRAY_A.
 *                                Default OBJECT.
 * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|void WP_Post on success.
 */
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
	global $wpdb;

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

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

get_page_by_path() 源文件

相关