
使用XAMPP本地搭建WordPress网站图文教程
get_page_by_title ( $page_title, $output = OBJECT, $post_type = 'page' )
检索一个页面的标题。
如果有多个文章使用相同的标题,将返回ID最小的那个文章。注意:如果有多篇文章具有相同的标题,它将检查最早的出版日期,而不是最小的ID。
因为这个函数使用MySQL的’=’比较,$page_title通常会被匹配为不区分大小写的默认排序。
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 ); }