get_page_by_title

函式
get_page_by_title ( $page_title, $output = OBJECT, $post_type = 'page' )
引數
  • (string) $page_title Page title.
    Required:
  • (string) $output 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 OBJECT.
    Required:
    Default: OBJECT
  • (string|array) $post_type Optional. Post type or array of post types. Default 'page'.
    Required:
    Default: 'page'
返回值
  • (WP_Post|array|null) WP_Post (or array) on success, or null on failure.
定義位置
相關方法
get_page_by_pathget_admin_page_titleget_the_titleget_page_childrenget_page_statuses
引入
2.1.0
棄用
-

get_page_by_title函式是一個WordPress的函式,通過它的標題來檢索單個頁面: 這個函式接受一個引數,它是你想檢索的頁面的標題: 該函式返回一個頁面物件。

檢索一個頁面的標題。

如果有多個文章使用相同的標題,將返回ID最小的那個文章。注意: 如果有多篇文章具有相同的標題,它將檢查最早的出版日期,而不是最小的ID。

因為這個函式使用MySQL的’=’比較,$page_title通常會被匹配為不區分大小寫的預設排序。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
$args = array(
'title' => $page_title,
'post_type' => $post_type,
'post_status' => get_post_stati(),
'posts_per_page' => 1,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'no_found_rows' => true,
'orderby' => 'post_date ID',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$pages = $query->posts;
if ( empty( $pages ) ) {
return null;
}
return get_post( $pages[0], $output );
}
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { $args = array( 'title' => $page_title, 'post_type' => $post_type, 'post_status' => get_post_stati(), 'posts_per_page' => 1, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'no_found_rows' => true, 'orderby' => 'post_date ID', 'order' => 'ASC', ); $query = new WP_Query( $args ); $pages = $query->posts; if ( empty( $pages ) ) { return null; } return get_post( $pages[0], $output ); }
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	$args  = array(
		'title'                  => $page_title,
		'post_type'              => $post_type,
		'post_status'            => get_post_stati(),
		'posts_per_page'         => 1,
		'update_post_term_cache' => false,
		'update_post_meta_cache' => false,
		'no_found_rows'          => true,
		'orderby'                => 'post_date ID',
		'order'                  => 'ASC',
	);
	$query = new WP_Query( $args );
	$pages = $query->posts;

	if ( empty( $pages ) ) {
		return null;
	}

	return get_post( $pages[0], $output );
}

常見問題

FAQs
檢視更多 >