仅主页页脚
我有一个博客,http://sweatingthebigstuff.com,我想在页脚中添加一行,该行将显示只能从主页上。我找到了这段代码,但它对我不起作用。我的语法是否错误,或者我可以尝试其他什么方法来使其正常工作吗?
<?php if ( is_home() ) { ?>
text
<?php } ?>
这是调用 footer.php 的地方
<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
<div class="cleared"></div>
<?php get_footer(); ?>
这是页脚代码:
文字2
联系我们 |免责声明 |隐私声明
版权所有 © 2009-2010 Sweating The Big Stuff。版权所有。
然后是一些站点流量计的废话。
I have a blog, http://sweatingthebigstuff.com and I would like to add an extra line in the footer which will display only from the homepage. I found this code, but it is not working for me. Do I have the wrong syntax or is there something else I can try to get this to work?
<?php if ( is_home() ) { ?>
text
<?php } ?>
Here is where footer.php is called
<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
<div class="cleared"></div>
<?php get_footer(); ?>
And here is the footer code:
text 2
Contact | Disclaimer | Privacy Statement
Copyright © 2009-2010 Sweating The Big Stuff. All Rights Reserved.
and then some sitemeter crap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
is_home() 设置了一个全局变量,它似乎不会重置自身或重新评估,wp 有点奇怪。
尝试将
wp_reset_query()
放在 if 语句代码的end开头实际上,最好先调用它,因为我们可以确保重置查询
现在 php 正在工作,理想情况下您需要上面的代码。
文本
我刚刚查看了源代码,我可以清楚地看到 php 代码,该代码不应该是可见的,因为它应该在服务器端进行解析。查看源代码中不应该出现以下内容...正在编辑错误的文件?
footer.php 文件应位于 wp-content/themes/nameofyourtheme 文件夹中
is_home()
sets a global var that doesn't seem to reset itself or re-evaluate, wp kinda strange.Try putting
wp_reset_query()
at theendstart of your if statement codeActually, it'll be better to call it before as we can ensure the queries are reset
Now that the php is working, ideally you'd want the code above.
text
I just did a view source and I can plainly see the php code, which shouldn't be visible since it is meant to be parsed server side. The following shouldn't be there in the view source.... wrong file being edited?
The footer.php file should be located in wp-content/themes/nameofyourtheme folder
is_home()
是一个应该返回 true 或 false 的方法。您需要在某处实现此方法。如果blogspot没有为你实现这个方法,你需要自己实现。对于您的网站,我认为这个函数可以满足您的要求:在您想要页脚的位置,放置:而
不是行:
我相信您的问题是 get_footer() 正在将页脚读取为文本,因此它不会执行预先PHP。如果您这样做,您可以根据需要在标头中添加任意数量的 PHP。
is_home()
is a method that should return true or false. You need to implement this method somewhere. If blogspot doesn't implement this method for you, you need to do it yourself. For your website I think this function would do what you want:And where you want the footer, put:
instead of the line:
I believe your problem is that get_footer() is reading the footer as text, so it isn't executing the PHP beforehand. If you do it this way you can add as much PHP in the header as you want.
您的语法没有任何问题,当我查看您的页面源代码时,我看到“文本”
您的页脚页面的文件扩展名是什么?如果它是“footer.php”,那么我不应该看到 php () 的开始和结束术语。除非文件扩展名是“.php”,否则 php 不会运行
(对于之前的答案):
应该在
text
在这种情况下。 is_home() 取决于页面上存在的循环。也许您在某个地方使用了自定义查询,或者您的某个插件使用了自定义查询,这扰乱了默认查询变量。就像我说的,在 if 语句之前使用重置语句。There is nothing wrong with your syntax, and when I view your page source, I see " text "
What is the file extension of your footer page? if it is "footer.php" then I shouldn't see the opening and closing terms for php (). php won't run unless the file extension is ".php"
as to a previous answer:
<?php wp_reset_query();?>
should go BEFORE<?php if(is_home()){?>text<?php } ?>
in this scenario. is_home() depends on a loop being present on the page. Maybe somewhere you used a custom query, or one of your plugins used a custom query that upset the default query vars. Like I said, use the reset statement before your if statement.您需要修改代码以包含对
is_front_page()
的检查,如下所示:参考:WordPress Codex:is_front_page()
默认 WP 安装没有定义主页,它使用您的 index.php 并检查其他模板(例如
home.php
模板)作为起点(home.php
仅当您的主题有它时。)这是他们在线文档中的图表,显示了其层次结构的工作原理:WordPress 模板层次结构。如果您想了解如何设置静态主页,请阅读本文并按照说明进行操作:创建静态首页。当您设置静态首页时,
is_home()
将按预期工作。You need to modify the code to include a check for
is_front_page()
like so:Reference: WordPress Codex: is_front_page()
Default WP installations don't have a homepage defined, it uses your index.php and checks for other templates like a
home.php
template as a starting point (home.php
only if your theme has it.) Here's a diagram from their online docs that shows how their hierarchy works: WordPress Template Hierarchy.If you want to know how to set a static homepage, read this article and follow the instructions: Creating a Static Frontpage. When you set a static front page,
is_home()
will work as expected.