2

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>
  • hi found this trend code supply above is what i am looking for but I have no idea why the '<?php echo $page->post_excerpt; ?>' is not working!! can you tell me what i miss! thank's in advance – jhunlio Jul 12 '13 at 8:47
6

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' );
| improve this answer | |
  • i didn't knew that function exists. I made similar function a while ago. I guess i don't need that anymore :) – Sisir Jul 31 '12 at 11:31
  • Thanks for helping! I will update my post with the complete code that solves the problem. – George Grigorita Jul 31 '12 at 12:37
  • glad i could help :) – pcarvalho Jul 31 '12 at 13:04
2

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.

| improve this answer | |
  • I didn't used a loop for this, would it made any real difference - performance wise - if I use a loop with this code or not? – George Grigorita Jul 31 '12 at 12:44
  • In this case, you'd have to recreate the whole functions internals - see the link in the answer ↑ above. – kaiser Jul 31 '12 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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