get_page_by_title()通过页面的标题获取页面信息

目录

描述

Retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.

Because this function uses the MySQL '=' comparison the $page_title will usually be matched as case insensitive with default collating.

get_page_by_title() 描述

用法


<?php 
  get_page_by_title
$page_title$output$post_type );
?>

get_page_by_title() 用法

参数

$page_title

(string) (必填) Page title

默认值: None

$output

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

默认值: OBJECT

$post_type

(string) (可选) Post type.

默认值: page

get_page_by_title() 参数

返回值

(mixed) 

OBJECT, ARRAY_N, or ARRAY_A.

NULL when no posts found.

get_page_by_title() 返回值

示例

Find Page ID to use with exclude in wp_list_pages

This example will return the $page object for the page titled "About". Then the $page->ID element is used to exclude the About page when listing pages.

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

<?php 
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>

How To Find WordPress Page ID By Title Then Replace the_content()

In this example, we find the page id of "Sample Page" then replace the page's the_content() with "Hello World!"

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

function my_content($content) {
    $page = get_page_by_title( 'Sample Page' );
    
    if ( is_page($page->ID) )
        $content = "Hello World!";
        return $content;
}

add_filter('the_content', 'my_content');

get_page_by_title() 示例

注意

  • 使用到 global: (object) $wpdb

get_page_by_title() 注意

历史

  • 添加于 版本: 2.1.0
  • 3.0.0: $post_type Parameter was added.

get_page_by_title() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title
 * @param string       $output     Optional. Output type. 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 or null on failure
 */
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	global $wpdb;

	if ( is_array( $post_type ) ) {
		$post_type = esc_sql( $post_type );
		$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
		$sql = $wpdb->prepare( "
			SELECT ID
			FROM $wpdb->posts
			WHERE post_title = %s
			AND post_type IN ($post_type_in_string)
		", $page_title );
	} else {
		$sql = $wpdb->prepare( "
			SELECT ID
			FROM $wpdb->posts
			WHERE post_title = %s
			AND post_type = %s
		", $page_title, $post_type );
	}

	$page = $wpdb->get_var( $sql );

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

get_page_by_title() 源文件

相关