Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post_excerpt instead of post_content, outside of the loop

Tags:

php

wordpress

I'm trying to get a wordpress excerpt from a page to appear in a styled area of my footer. I currently have the following, which gives me the title and all of the content:

<?php
$page_id = 2;
$page_data = get_page( $page_id );
$content = $page_data->post_content;
$title = $page_data->post_title;
echo '<h3>'. $page_data->post_title .'</h3>';
echo '<p>'. $page_data->post_content .'</p>'; ?>

I've tried various combinations of post_excerpt instead of post_content, and have tried emulating and editing the example here: http://codex.wordpress.org/Function_Reference/get_page but am not having any luck. A few times I've tried examples from other people, but there has been no content at all.

Could this be because I'm trying to get it to display outside of the loop, or have I just not hit the right combination yet?

Thanks.

like image 705
Matt Avatar asked Feb 18 '23 22:02

Matt


1 Answers

Why don't you just limit the string using substr()?

$content = $page_data->post_content;
$excerpt = substr($content, 0, 155);

something to that effect

at that rate, you don't need to try and find the true variable of the excerpt, and you can control the length, and whether or not it has ellipses at the end, etc.

Also, if you NEED to find the excerpt variables, you could just do a var_dump($page_data), and see what returns the value you need, no?

EDIT:

you could try adding a manual excerpt to the pages using a function like this in functions.php

add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
    add_post_type_support( 'page', 'excerpt' );
}

EDIT on an EDIT:

So I just dug a little deeper, and this little guy right here might help you out. Drop this in your functions.php file (source)

add_post_type_support( 'page', 'excerpt' );
like image 196
Xhynk Avatar answered Feb 27 '23 11:02

Xhynk