DISbeat
DISbeat

Reputation: 101

Wordpress - PHP if_page - change image on every page

I need some help with wordpress. My english is not the best, so i hope you understand me.

I have a own wordpress theme with a little headline picture which should change on every page you open. For Example, if you go on start page, i want to show a picture with "START", or if you go on contact page, i want to show a picture with "CONTACT".

Now i found a PHP script:

            <?php
            // TOP PICTURE DEFINITIONS
            if ( is_page('weingut') ) {
            $toppic = 'weingut.png';
            }
            if ( is_page('39') ) {
            $toppic = 'anbau.png';
            }
            if ( is_page('lagen') ) {
            $toppic = 'lagen.png';
            }
            if ( is_page('weine') ) {
            $toppic = 'weine.png';
            }
            if ( is_page('macher') ) {
            $toppic = 'macher.png';
            }
            else {
            $toppic = 'weingut.png';
        }
        ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/titel/<?php echo $toppic ?>" alt="unser-weingut" class="img-responsive" />

But this doesn't work. tried it with page ID like "39" and i tried it with page title like "weingut", but on each page it just show me the "ELSE" picture from this part of the code:

            else {
            $toppic = 'weingut.png';

Would be amazing if you could help me ;D

Upvotes: 0

Views: 52

Answers (1)

Manish Shukla
Manish Shukla

Reputation: 1365

The issue is in you coding format. In you logic you have matched each page with if condition and last if has else block also your code is initially setting correct value but when it reaches at the last if block and if it return false it reset previous set value. So instead of using multiple if condition you should use if else ladder.

Upvotes: 1

Related Questions