Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Thanks! Amit

like image 541
Amit Avatar asked Nov 06 '25 17:11

Amit


1 Answers

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!

like image 139
Amit Avatar answered Nov 09 '25 10:11

Amit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!