^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_page_by_title()用法示例

get_page_by_title( string $page_title,  string $output = OBJECT,  string|array $post_type = 'page' )

Retrieve a page given its title.


描述


参数

$page_title

(string)
(Required)
Page title

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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

更新日志

Version 描述
2.1.0 Introduced.

相关函数

Uses

  • wp-includes/formatting.php:
    esc_sql()
  • wp-includes/post.php:
    get_post()
  • wp-includes/wp-db.php:
    wpdb::get_var()
  • wp-includes/wp-db.php:
    wpdb::prepare()

  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

    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.

    
    <?php 
    $page = get_page_by_title( 'About' );
    wp_list_pages( 'exclude=' . $page->ID );
    ?>
    
  2. 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!”

    
    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');
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈