使用 if is_page() Wordpress 条件语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3381047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:37:06  来源:igfitidea点击:

Using if is_page() Wordpress conditional statement

wordpresstagsconditionalif-statement

提问by Amit

I'm trying to have different pictures on every one of my pages built on wordpress.

我试图在我基于 wordpress 的每一页上都有不同的图片。

So I have the following in my index.php file, archive.php file, page.php file, etc:

所以我的 index.php 文件、archive.php 文件、page.php 文件等中有以下内容:

<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic; ?>" alt="page1" id="mainPageImg" />

Now, in my page.php file, I have the following:

现在,在我的 page.php 文件中,我有以下内容:

<?php
    // TOP PICTURE DEFINITIONS
    if ( is_home() ) {
        $toppic == 'page1.png';
    }
    if ( is_page('articles') ) {
        $toppic == 'page2.png';
    }
?>

How come this does not work? I tried it with one equal (=) sign...

这怎么行不通?我用一个等号 (=) 试过了...

EDIT: If I define $toppic at the top, for example, in the index.php file as follows:

编辑:如果我在顶部定义 $toppic,例如,在 index.php 文件中,如下所示:

<?php $toppic = 'page1.png'; ?>

Then it works. So therefore, it must be something that has to do with the conditional if is_page/is_home statements. Any ideas?

然后它起作用了。因此,它必须与条件 if is_page/is_home 语句有关。有任何想法吗?

Thanks! Amit

谢谢!阿米特

回答by Amit

Okay, I found the answer.

好的,我找到了答案。

This is what needs to be done. For the articles (blog) page, at the top section you need to place the following:

这是需要做的。对于文章(博客)页面,您需要在顶部放置以下内容:

<?php // TOP PICTURE DEFINITION FOR ARTICLES PAGE
        if ( is_home() ) {
            $toppic = 'page1.png';
        }
?>

Then, in your page.php file, you can control the picture at the top for all other pages (except 404, where you'd need to put a is_404() in your 404.php. So this is what it looks like:

然后,在你的 page.php 文件中,你可以控制所有其他页面顶部的图片(除了 404,你需要在 404.php 中放置一个 is_404()。所以它是这样的:

<?php
    // TOP PICTURE DEFINITIONS
    if ( is_page('english') ) {
        $toppic = 'page1.png';
    }
    if ( is_page('aboutus') ) {
        $toppic = 'page1.png';
    }
    if ( is_page('newspaper') ) {
        $toppic = 'page1.png';
    }
    else {
        $toppic = 'page1.png';
    }
?>

And finally, in order to implement this, use the following HTML/php syntax:

最后,为了实现这一点,请使用以下 HTML/php 语法:

<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic ?>" alt="page1" id="mainPageImg" />

That's all. Phew. Finally got it to work :) Had to do it for a client, too!

就这样。呼。终于让它工作了:) 也必须为客户做!

回答by Jesse Dhillon

The slug for your Articles page has to be defined as articles. This is set in the edit page interface, see these directions.

您的文章页面的 slug 必须定义为articles. 这是在编辑页面界面中设置的,参见这些说明