I have the following setup: the front-page is setup as ‘static’ and it uses a theme page template. On this template / front-page, I need to get the page title, URL and excerpt of the About page.
I found this code that does exactly what I need, but I’m wandering if there’s a different approach to this, one that would not use the page ID but the page slug or title.
<?php
$page_id = 13;
$page_data = get_page( $page_id );
$the_excerpt = $page_data->post_excerpt;
$title = $page_data->post_title;
?>
<a href="<?php echo get_permalink(13); ?>">
<?php echo $page_data->post_title; ?>
</a>
Later edit: Based on the answer provided by peteroak, this is the complete working code:
<?php
$page = get_page_by_title( 'About' );
$the_excerpt = $page->post_excerpt;
$page_data = get_page( $page );
$title = $page_data->post_title;
?>
<header class="entry-header">
<h1 class="entry-title">
<a href="<?php echo esc_url( get_permalink( get_page_by_title( 'About' ) ) ); ?>">
<?php echo $page_data->post_title; ?>
</a>
</h1>
</header>
<div class="entry-content"><?php echo $page->post_excerpt; ?>
<a href="<?php echo esc_url( get_permalink( get_page_by_title( 'About' ) ) ); ?>">more +</a>
</div>
the codex has exacly what you need:
get_page_by_title()
Example
or
Use the public API everywhere
Inside the loop, you can use
get_the_excerpt()
(without arguments). It’s better than$post/$page->excerpt
, as it also attaches the filters and checks if the page/post has a password.