views:

44

answers:

2

Hi all, 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

A: 

I think what you are looking for is:

$stringa = get_the_content();
Sabeen Malik
no it doesn't work
simone
+1  A: 

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.

EAMann
thanks a lot! Bye
simone