-1

I'm trying to fetch content about the current post into the header.php of my theme (to dynamically insert Facebook OpenGraph Metadata).

Since I'm on a single post page, shouldn't it be able to run functions like the_content() or wp_get_attachment_thumb_url()?

<?php if (is_single()) { 
          <meta property="og:title" content="<?php echo get_the_title()." - Advocate Daily" ?>" />
          <meta property="og:description" content="<?php string_limit_words(the_content(), 15);" ?> />
          <meta property="og:image" content="<?php echo wp_get_attachment_thumb_url(); ?>" />
<?php } ?>

In the above example, the first tag works, but the content of the other two isn't outputted.

Does this mean certain template tags needs to be in the

if ( have_posts() ) : while ( have_posts() ) : the_post();

loop? Do I need to declare this loop again in the header?

1

Consider this snippet here to accomplish your task. If you add it to your functions.php file, the Open Graph tags will be populated automatically as part of the wp_head action.

2

First line of the codex for get_content:

Displays the contents of the current post. This template tag must be within The_Loop.

Also, this line:

<meta property="og:description" content="<?php string_limit_words(the_content(), 15);" ?> />

would need to get_the_content(), as the_content() will output, and you need to return.

<meta property="og:description" content="<?php echo string_limit_words(get_the_content(), 15);" ?> />
  • Thanks, I saw that in Codex too, and I originally tried it with get_the_content() and the_content(), but it doesn't work. Neither does wp_get_attachment_thumb_url(). – Orun May 8 '13 at 16:30

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.