Ready to get started?Download WordPress


How to have a different picture on specfied pages using conditional if is_page (3 posts)

  1. engineercreativity
    Posted 3 years ago #

    Hi all,

    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:

        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

  2. Kapil Chugh
    Posted 3 years ago #

    Rather than defining this code on each page you should define this code in a function and call that function

    for eg.in functions.php

    function get_page_image() {
    if (is_home()) {
    $topic = 'page.png';
    } else if ( is_single() ) {//article page
    $topic = 'page1.png';
    } else if ( is_page() ) {
    $topic = 'page2.png';

    } else if ( is_category() ) {
    $topic = 'page3.png';

    } else if ( is_tag() ) {
    return $topic;

    and call this function on header.php

  3. engineercreativity
    Posted 3 years ago #

    Ahh...very very clever sir. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic