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="https://wordpress.stackexchange.com/questions/60304/<?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="https://wordpress.stackexchange.com/questions/60304/<?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="https://wordpress.stackexchange.com/questions/60304/<?php echo esc_url( get_permalink( get_page_by_title("About' ) ) ); ?>">more +</a>
    </div>

2 Answers
2

the codex has exacly what you need: get_page_by_title()

Example

 $page = get_page_by_title( 'About' );
 $the_excerpt = $page->post_excerpt;

or

 $page = get_page_by_path( 'parent-page/sub-page' );

Leave a Reply

Your email address will not be published. Required fields are marked *