0

So, I have a foreach loop within get_pages that shows a list of child pages within a parent ID.

The titles, featured image, and links come through fine, however I can't get excerpts to show up at all. I'm referencing http://codex.wordpress.org/Function_Reference/get_pages

$args = array(
    'child_of'     => 16,
    'title_li'     => '',
    'depth'         => 0,
    'sort_order'    => 'DESC',
    'sort_column'   => 'menu_order'
);
$pages = get_pages( $args );
$output = '';
foreach($pages as $value){
    $thumb = get_the_post_thumbnail( $value->ID, $attr = '' );
    $output .= "<div class='case-studies-parent'><div class='inner'>";
    $output .= "<div class='image'><a href='" . $value->post_name . "' >" . $thumb . "</a>    </div>";
    $output .= "<div class='title'><a href='" . $value->post_name . "' >" .  $value->post_title . "</a></div>";
    $output .= "<div class='excerpt'>". $value->post_excerpt . "</div>";
    $output .= "</div></div>";
} 
echo $output;

I can change $value->post_excerpt to $value->post_content and that works correctly, showing the entire page content.

I've also tried with various variations including get_the_excerpt(), the_excerpt() and post_excerpt but to no avail.

Is there something I'm missing? On the Codex page, it has post_excerpt listed as a correct return, so I'm not really sure why it's not showing. (By the way, yes I have added the MORE tag to my pages)

UPDATE: var_dump($value)

object(WP_Post)[276]
public 'ID' => int 155
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2014-08-07 09:40:53' (length=19)
public 'post_date_gmt' => string '2014-08-07 09:40:53' (length=19)
public 'post_content' => string '<h2>Mobile phone tracking</h2>
<p><span>Parents keen to know the location of their children, and businesses seeking to ensure safety of lone workers have since 2004, been able to purchase a mobile phone tracking service, designed and implemented by InverOak, in conjunction with Cybit for The Carphone Warehouse.</span></p>
<p>The Carphone Warehouse, Europe's leading independent retailer of mobile phones and services, asked InverOak to design a system that tracks the location of any mobile phone in the UK, '... (length=2656)
public 'post_title' => string 'The Carphone Warehouse' (length=22)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'the-carphone-warehouse' (length=22)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2014-08-11 10:01:34' (length=19)
public 'post_modified_gmt' => string '2014-08-11 10:01:34' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 16
public 'guid' => string 'http://inveroak.local/?page_id=155' (length=34)
public 'menu_order' => int 0
public 'post_type' => string 'page' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)

So, the post_excerpt is empty. But I don't fully understand why, if it's included in the return on the codex?

UPDATE: So I've discovered the excerpt for pages was removed in 3.1 and have found a manual excerpt function added to the theme functions.php file:

add_post_type_support( 'page', 'excerpt' );

While this seems to work okay, is there any difference between this, and using the tag in the editor? Which is cleaner?

  • you could try a var_dump of $value inside the foreach. But if I remember correctly, in this case the_excerpt refers to the manual except, which probably is empty. Paste the result of the var_dump in the question. – Tomás Cot Aug 11 '14 at 11:48
  • I think you should use this function wp_trim_words(). – Tomás Cot Aug 11 '14 at 12:22
  • Yeah that seems to work quite nicely without needing to add anything extra. Can you add it as an answer? – Lee Aug 11 '14 at 12:40
  • Have you checkd if it strips all the tags in the content? It should, but I don't remember. – Tomás Cot Aug 11 '14 at 12:44
  • Yeah it does strip all tags. – Lee Aug 11 '14 at 13:41
0

You can use this function wp_trim_words(). It allows you to format the output, and strips all the HTML tags in the content (it uses wp_strip_all_tags).

You will have to pass post_content as the first paramenter.

-2

Has you are requesting the excerpt of every post inside your query, you need to setup your post data for each post.

$args = array(
    'child_of'     => 16,
    'title_li'     => '',
    'depth'         => 0,
    'sort_order'    => 'DESC',
    'sort_column'   => 'menu_order'
);
$pages = get_pages( $args );
$output = '';
foreach($pages as $value){

    $temp = $post;
    $post = get_post( $value->ID );
    setup_postdata( $post );
    $thumb = get_the_post_thumbnail( $value->ID, $attr = '' );
    $output .= "<div class='case-studies-parent'><div class='inner'>";
    $output .= "<div class='image'><a href='" . $value->post_name . "' >" . $thumb . "</a>    </div>";
    $output .= "<div class='title'><a href='" . $value->post_name . "' >" .  $value->post_title . "</a></div>";
    $output .= "<div class='excerpt'>". echo get_excerpt(190) . "</div>";
    $output .= "</div></div>";
    wp_reset_postdata();
    $post = $temp;
} 
echo $output;

Ps: do you have any content on your excerpt? maybe the code is on your page, have you check your css to review if the excerpt isn't hidden?

  • Please reread the question. The OP was updated to include the fact that the_excerpt() tag was removed from pages as of version 3.1 – Pieter Goosen Aug 11 '14 at 12:47
  • Thanks for your comment, yeah it wasn't a case of the excerpt being hidden, it wasn't being generated at all. So I just used the main content, and truncated it using the wp_trim_words function as mentioned above – Lee Aug 11 '14 at 13:45

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.