I'd like to have the content of the current single post loaded into the og:description meta property - but the '. $content .' doesn't output anything?

This is what's in my header.php

if (is_single()) {

$content = get_the_content();
$desc='<meta property="og:description" content="'. $content .'" />';
echo $desc;

What could the problem be?


2 Answers 2


get_the_content() must be inside the loop, in header.php you can do this (don't forget to scape the content to use it as attribute):

 if (is_single()) {
    while (have_posts()) {
        $content = get_the_content();
        $desc='<meta property="og:description" content="'. esc_attr($content) .'">';
        echo $desc;

or even better, in your functions.php hook the wp_head action; also, I recomend using the excerpt instead of the content as descriptoin. (note the use of global $post and setup_postdata).

 add_action( 'wp_head', 'my_wp_head' );
 function my_wp_head() {
     if (is_single()) {
         $post_id = get_queried_object_id():
         $excerpt = get_the_excerpt( $post_id );
         $desc = '<meta property="og:description" content="Blabla'. esc_attr( $excerpt ) .'">';
         echo $desc;
      //More stuff to put in <head>

See the codex page for get_the_content():

Retrieve the post content. (Must be used in a Loop)

Emphasis on »Must be used in a Loop«.


To give you a possible solution, you can use get_post(), for pages and posts, like shown below, to get the post content outside the loop.

$post_id = get_queried_object_id();
$post_obj = get_post( $post_id );
$content = $post_obj->post_content;


There is a pretty interesting comparison by Peter Knight regarding the Differences between using get_post() and WP_Query(). I can't replicate it all, but basically it boils down to: performance and amount of available data. A short not at all conclusive and complete aggregation could be: on the one hand, with WP_Query you have the meta data too, not the case with get_post, but on the other hand, with WP_Query there are four database queries, whereas you have one database query using get_post. So there definitely are some differences to consider, read the article for more information, which method is right depends on what is needed for the actual use case on hand.

  • This i imagine, is precisely the problem. +1
    – t31os
    Feb 3, 2014 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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