如何根据Wordpress主题中的页面ID更改图像?

时间:2014-05-01 20:38:50

标签: php wordpress themes

我正在构建一个WordPress主题&我试图让我的功能区域中的图像根据用户所在的页面而改变。

我有一段功能文本的代码,可以使用:

            <?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>";
            ?>

然后是图像:

            <?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">';
            ?>

目前,看起来它似乎想要工作,但图像&#39;回形针&#39;没有正确访问根目录。

任何人都能接受我出错的地方吗?我是PHP的绝对初学者,所以请原谅我,如果它真的很明显!

谢谢!

1 个答案:

答案 0 :(得分:0)

根据get_the_ID()上的WordPress Codex

  

检索当前帖子的数字ID。此标记必须为   循环

我目前看不到你的循环代码,所以如果你不在循环之外就可以使用以下内容:

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