仅主页页脚

发布于 2024-10-01 08:01:33 字数 580 浏览 5 评论 0原文

我有一个博客,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲸落 2024-10-08 08:01:33

is_home() 设置了一个全局变量,它似乎不会重置自身或重新评估,wp 有点奇怪。

尝试将 wp_reset_query() 放在 if 语句代码的 end 开头

实际上,最好先调用它,因为我们可以确保重置查询

<?php wp_reset_query();
if ( is_home() ) { ?>
text
<?php }  ?>

现在 php 正在工作,理想情况下您需要上面的代码。

文本

我刚刚查看了源代码,我可以清楚地看到 php 代码,该代码不应该是可见的,因为它应该在服务器端进行解析。查看源代码中不应该出现以下内容...正在编辑错误的文件?

<p>
<?php if ( is_home() ) { ?> 
text 
<?php } ?>
<wp_reset_query()>
<br />
<br />

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 the end start of your if statement code

Actually, it'll be better to call it before as we can ensure the queries are reset

<?php wp_reset_query();
if ( is_home() ) { ?>
text
<?php }  ?>

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?

<p>
<?php if ( is_home() ) { ?> 
text 
<?php } ?>
<wp_reset_query()>
<br />
<br />

The footer.php file should be located in wp-content/themes/nameofyourtheme folder

空‖城人不在 2024-10-08 08:01:33

is_home() 是一个应该返回 true 或 false 的方法。您需要在某处实现此方法。如果blogspot没有为你实现这个方法,你需要自己实现。对于您的网站,我认为这个函数可以满足您的要求:

<?php
function is_home(){
    $r = $_SERVER['REQUEST_URI'];
    return $r == '/' || $r == '' || $r == '/index.php';
}

if(is_home()) {
?>
text
<?php } ?>

在您想要页脚的位置,放置:而

<?php include 'footer.php'; ?>

不是行:

<?php get_footer(); ?>

我相信您的问题是 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:

<?php
function is_home(){
    $r = $_SERVER['REQUEST_URI'];
    return $r == '/' || $r == '' || $r == '/index.php';
}

if(is_home()) {
?>
text
<?php } ?>

And where you want the footer, put:

<?php include 'footer.php'; ?>

instead of the line:

<?php get_footer(); ?>

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.

眼泪淡了忧伤 2024-10-08 08:01:33

您的语法没有任何问题,当我查看您的页面源代码时,我看到“文本”

您的页脚页面的文件扩展名是什么?如果它是“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.

愛上了 2024-10-08 08:01:33

您需要修改代码以包含对 is_front_page() 的检查,如下所示:

<?php
    $ishomepage = ( is_home() || is_front_page() );
    switch( $ishomepage )
    {
        case true :
            echo 'Your homepage-only snippet of text goes here';
            break;

        case false :
        default :
            // Do nothing... or do something else...
            break;
    }

?>

参考:WordPress Codex:is_front_page()

默认 WP 安装没有定义主页,它使用您的 index.php 并检查其他模板(例如 home.php 模板)作为起点(home.php 仅当您的主题有它时。)这是他们在线文档中的图表,显示了其层次结构的工作原理:WordPress 模板层次结构

默认情况下,WordPress 在首页上显示您最近的帖子
您网站的。但许多 WordPress 用户希望拥有一个静态的前台
页面或启动页面作为首页。这个“静前
对于那些不希望有“博客”外观的人来说,“页面”外观很常见
他们的网站,给人一种更 CMS(内容管理系统)的感觉。

如果您想了解如何设置静态主页,请阅读本文并按照说明进行操作:创建静态首页。当您设置静态首页时,is_home() 将按预期工作。

You need to modify the code to include a check for is_front_page() like so:

<?php
    $ishomepage = ( is_home() || is_front_page() );
    switch( $ishomepage )
    {
        case true :
            echo 'Your homepage-only snippet of text goes here';
            break;

        case false :
        default :
            // Do nothing... or do something else...
            break;
    }

?>

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.

By default, WordPress shows your most recent posts on the front page
of your site. But many WordPress users want to have a static front
page or splash page as the front page instead. This "static front
page" look is common for those who wish to not have a "blog" look to
their site, giving it a more CMS (content management system) feel.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文