admin管理员组文章数量:1125064
I'm using the following code to insert the content from another wp page into my main home page.
Is it possible to make it work with a page title instead of an id number?
Eg. Not $page_id = 518 but $page_id = 'about' instead????
$page_id = 518; //Your Page ID
$page_data = get_page( $page_id );
// Displays the title
echo '<h1>'. $page_data->post_title .'</h1>';
// Displays the content
echo apply_filters('the_content', $page_data->post_content);
?>
I'm using the following code to insert the content from another wp page into my main home page.
Is it possible to make it work with a page title instead of an id number?
Eg. Not $page_id = 518 but $page_id = 'about' instead????
$page_id = 518; //Your Page ID
$page_data = get_page( $page_id );
// Displays the title
echo '<h1>'. $page_data->post_title .'</h1>';
// Displays the content
echo apply_filters('the_content', $page_data->post_content);
?>
Share
Improve this question
asked May 25, 2013 at 8:18
speedypancakespeedypancake
5552 gold badges10 silver badges26 bronze badges
0
2 Answers
Reset to default 6There is a function exactly for that:
get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' );
You can use it like this:
$page = get_page_by_title( 'Start' );
To get the page from a specific post type:
$custom = get_page_by_title( 'Start', OBJECT, 'your_custom_post_type' );
$post = get_page_by_title( 'Start', OBJECT, 'post' );
Be aware, this function will search in all post statuses. So you might get a draft, a trashed or private post. You should check the result with:
$status = get_post_status( $page );
if ( 'publish' !== $status )
return; // do not show unpublished posts
A related function is get_page_by_path()
:
$page = get_page_by_path( 'about/contact' );
Function get_page_by_title()
is deprecated. Use WP_Query
instead or try something like this:
/**
* Retrieves a link to a page object based on page title
* @param $title string Page title
* @return $link Page object permalink
*/
function get_page_object( $title ) {
$array_of_objects = get_posts([
'title' => $title,
'post_type' => 'page',
]);
$id = $array_of_objects[0];
$id = $id->ID;
$link = get_permalink($id);
return $link;
}
本文标签: Get page id by title
版权声明:本文标题:Get page id by title? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736658612a1946318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论