2010-10-13 62 views
1

我必須將帖子內容放入標籤<head>。 我這個代碼到我的主題的header.php文件嘗試:將帖子內容放入標題

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

,但它不工作。

我該怎麼辦? 感謝

+0

編輯了評論。 不可見 – simone 2010-10-13 15:05:46

回答

6

功能the_content()get_the_content()都意味着WordPress loop內使用,這意味着隨意,你不能只使用它們。您需要在header.php文件中構建一個循環,用於查詢WordPress數據庫,獲取一些內容並根據需要使用它。

基本上,裏面包裹您的通話the_content()

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

然後你就可以在頁面上的任何地方獲取文章內容...但是,我不明白爲什麼你想獲取頁面內<head>部分的帖子內容。 <head>用於樣式聲明,<script>標籤和有關頁面的元信息... 不是用於實際頁面內容。如果你想獲得關於當前頁面的特定信息,我建議完全使用一個不同的函數。

+0

非常感謝! Bye – simone 2010-10-13 18:14:04

+0

嗯----我不知道最初問這個問題的人 - 但是如果我要成功地製作Twitter卡,我需要發佈內容。 – 2017-05-26 23:23:06

-1

我知道你在尋找的是:

$stringa = get_the_content(); 
+0

不,它不起作用 – simone 2010-10-13 14:29:51

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

重新放置rewind_posts(),否則後循環將無法在其他模板中工作。