simone
simone

Reputation: 811

get post content into header

i have to get the post content into the tag <head>. I was trying with this code into the header.php file of my theme:

if(is_single()){
$stringa = the_content();
}

but it doesn't work.

how can i do? thanks

Upvotes: 2

Views: 4274

Answers (3)

Kuba Markiewicz
Kuba Markiewicz

Reputation: 11

if (is_single()) 
{
  the_post();
  $content = get_the_content();
  rewind_posts();
}

It's important to put rewind_posts(), otherwise post loop will not work in other templates.

Upvotes: -1

EAMann
EAMann

Reputation: 4146

The functions the_content() and get_the_content() are meant to be used inside the WordPress loop, which means you can't just use them at will. You'll need to build a loop inside your header.php file that queries the WordPress database, fetches some content, and uses it as necessary.

Basically, wrap your the_content() call inside:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    ...
<?php endwhile; endif; ?>

Then you'll be able to fetch post content anywhere on the page ... however, I don't quite understand why you're trying to get the post content inside the <head> section of the page. <head> is used for style declarations, <script> tags, and meta information about the page ... not for actual page content. If you're trying to get specific information about the current page, I'd recommend using a different function entirely.

Upvotes: 7

Sabeen Malik
Sabeen Malik

Reputation: 10880

I think what you are looking for is:

$stringa = get_the_content();

Upvotes: -1

Related Questions