WordPress根据页面标题获取页面对象函数:get_page_by_title

WordPress函数get_page_by_title根据页面标题获取页面对象,如果有相同名称标题的页面,则返回最早发布的页面。

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

函数参数

$page_title

字符串

页面标题

$output

字符串

返回数据的类型,可用值如下:

  • OBJECT:返回对象;
  • ARRAY_A:返回关联数组;
  • ARRAY_N:返回数值数组;

$post_type

字符串,默认值:page

文章类型

函数返回值

WP_Post Object
(
    [ID] => 2
    [post_author] => 1
    [post_date] => 2020-11-27 11:37:03
    [post_date_gmt] => 2020-11-27 03:37:03
    [post_content] =>
    [post_title] => 关于我们
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => about
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2020-11-27 14:45:21
    [post_modified_gmt] => 2020-11-27 06:45:21
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => https://www.beizigen.com/?page_id=2
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

函数使用示例

$pages = get_page_by_title('关于我们');
echo '<a href="https://www.dianjin123.com/wp-tutorial/wordpressjiaocheng/76354.html' . get_page_link($pages->ID) . '">' . $pages->post_title . '</a>';
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » WordPress根据页面标题获取页面对象函数:get_page_by_title