2014-05-01 74 views
0

我正在构建一个WordPress主题&我试图根据用户所处的页面来改变我的功能区域中的图像。如何根据WordPress主题中的页面ID更改图像?

我有这样一段代码为特征的文字,其工作原理:

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

其中,此刻,看起来要工作,但图像'回形针'没有正确地在根目录中访问。

任何人都可以拿起我要去哪里错了吗?我是一个绝对的PHP初学者,所以请原谅,如果它真的很明显!

谢谢!

回答

0

按照WordPress Codexget_the_ID()

检索当前后的数字ID。该标签必须是之内 The Loop

我目前没有看到你代码的循环,所以如果你是外循环,你可以使用这样的事情:

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