Using if is_page() Wordpress conditional statement

53,734

Solution 1

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!

Solution 2

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

Share:
53,734
Amit
Author by

Amit

Please check out my website for more info.

Updated on August 01, 2020

Comments

  • Amit
    Amit almost 4 years

    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 do with the conditional if is_page/is_home statements. Any ideas?

    Thanks! Amit