get post content into header
i have to get the post content into the tag <head>
.
I was trying with this code into t开发者_Go百科he header.php file of my theme:
if(is_single()){
$stringa = the_content();
}
but it doesn't work.
how can i do? thanks
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.
I think what you are looking for is:
$stringa = get_the_content();
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.
精彩评论