简体   繁体   中英

How do I change an image depending on the page ID in Wordpress Theme?

I'm building a Wordpress Theme & I'm trying to make the image in my feature area change depending on which page the user is on.

I have this piece of code for the feature text, which works:

            <?php
                $pageID = get_the_ID();
                if ($pageID == 17)  // Overview
                    $text = "Greater numbers of young adults on the autism spectrum will gain access to higher 
                            education. Educational opportunities for people with autism spectrum disorders will improve across 
                            Europe.";
                else if ($pageID == 39)  //Publications
                    $text = "Our aim is to support the young students to learn the skills they need to negotiate student life and studies 
                            successfully";
                else if ($pageID == 44)  //Events
                    $text = "xxxxx";
                else if ($pageID == 37)  //Blog
                    $text = "xxxxx";
                else
                    $text = "We research, design, build and evaluate materials to help people with autism access and succeed within Higher Education.";
                echo "<h1>" . $text . "</h1>";
            ?>

And then this for the images:

            <?php 
            $pageID = get_the_ID();
            if ($pageID == 15) // Home Page
                echo
                '<img src="<?php echo get_template_directory_uri(); ?>/images/document.png" width="200" height="200" alt="doc">';
                else if ($pageID == 17) // Overview
                echo
                '<img src="<?get_template_directory_uri(); ?>/images/paperclip.png" width="200" height="200" alt="paperclip">';
            ?>

Which, at the moment, looks like it wants to work, but that the image 'paperclip' is not being accessed in the root properly.

Can anyone pick up on where I'm going wrong? I'm an absolute beginner with PHP, so please excuse me if it's really obvious!

Thanks!

According to the WordPress Codex on get_the_ID() :

Retrieve the numeric ID of the current post. This tag must be within The Loop .

I currently don't see a loop in you're code so if you're outside the loop you can use something like this:

global $post;
if($post->ID == 17){etc...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM