开发者

Using if is_page() Wordpress conditional statement

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

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

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

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

<?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:

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

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

Thanks! 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:

<?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:

<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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜