Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
Fantastic help - thanks toscho :-) –  speedypancake May 25 '13 at 8:40

1 Answer 1

up vote 4 down vote accepted

There 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' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.