将帖子内容放入标题中
我必须将帖子内容放入标签 中。 我尝试将此代码放入主题的 header.php 文件中:
if(is_single()){
$stringa = the_content();
}
但它不起作用。
我该怎么办? 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
函数
the_content()
和get_the_content()
旨在在 WordPress循环,这意味着你不能随意使用它们。您需要在header.php
文件中构建一个循环,用于查询 WordPress 数据库、获取一些内容并根据需要使用它。基本上,将您的
the_content()
调用包装在里面:然后您将能够在页面上的任何位置获取帖子内容...但是,我不太明白为什么您要尝试获取在页面的
部分内发布内容。
用于样式声明、
标记以及有关页面的元信息...不用于实际页面内容。如果您想获取有关当前页面的特定信息,我建议您使用完全不同的函数。
The functions
the_content()
andget_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 yourheader.php
file that queries the WordPress database, fetches some content, and uses it as necessary.Basically, wrap your
the_content()
call inside: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:
放置 rewind_posts() 很重要,否则 post 循环将无法在其他模板中工作。
It's important to put rewind_posts(), otherwise post loop will not work in other templates.