2016-12-26 70 views
0

所以我想在PHP if语句中返回一行HTML,但由于某些原因它没有执行。WordPress/PHP if语句没有执行

<?php 

        $args_commercial = array(
         'post_type' => 'sidebar_post' 
         ); 

        $query_commercial = new WP_Query($args_commercial); 

       ?> 

       <ul> 

       <?php if (have_posts()) : while ($query_commercial->have_posts()) : $query_commercial->the_post(); ?> 


        <?php 

        $thumb_id = get_post_thumbnail_id(); 
        $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true); 
        $thumb_url = $thumb_url_array[0]; 

        ?> 


        <li data-id="0"> 

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

        <?php 

        $page_field = strtolower(get_field('page')); 
        $page_title = strtolower(get_the_title()); 

        if ($page_field == $page_title): ?> 

         <a href="<?php echo get_field('link'); ?>" style="background-image: url(<?php echo $thumb_url; ?>)"><span><?php the_title(); ?></span></a> 

        <?php endif; ?> 

        </li> 


       <?php endwhile; endif; ?> 
       <?php endwhile; endif; ?> 

       </ul> 

所以才来解释,我是一个自定义后型(职位侧边栏)内循环和自定义后类型中是选择你要显示的自定义后类型的页面的选项。

这就是$ page_field变量代表的内容。 $ page_title只是页面的标题。因此,理论上,如果用户选择的页面字段和页面的实际标题相同,则将打印HTML,然后显示侧边栏。

由于某些原因,即使$ page_title和$ page_field是完全相同的值(我通过回显它们来测试它们),if块中的HTML仍未执行。

我真的不知道如何进一步调试,并确保HTML显示条件。任何帮助将大大appreacited

谢谢。

+1

var_dump($ page_field);后续代码var_dump($ PAGE_TITLE);确保这些值与您所期望的相同。 – Kaylined

+0

@Kaylined我var_dumped他们,他们都打印字符串(0)“”字符串(22)“商业包机” –

+0

所以你的第一个变量是空的。如果(“”==“商业包机”)无效。 – Kaylined

回答

0

看起来像循环是搞砸了我。我先浏览页面本身,获取页面标题,然后在后面遍历自定义文章类型。这让if块执行。这里是代码:

<?php 

        $args_commercial = array(
         'post_type' => 'sidebar_post' 
         ); 

        $query_commercial = new WP_Query($args_commercial); 

       ?> 

       <ul> 

       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

       <?php 

       $page_title = strtolower(get_the_title()); 

       ?> 

       <?php endwhile; endif; ?> 

       <?php if (have_posts()) : while ($query_commercial->have_posts()) : $query_commercial->the_post(); ?> 


        <?php 

        $thumb_id = get_post_thumbnail_id(); 
        $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true); 
        $thumb_url = $thumb_url_array[0]; 

        ?> 


        <li data-id="0"> 

        <?php 

        $page_field = strtolower(get_field('page')); 

        if (trim($page_field) == trim($page_title)): ?> 

         <a href="<?php echo get_field('link'); ?>" style="background-image: url(<?php echo $thumb_url; ?>)"><span><?php the_title(); ?></span></a> 

        <?php endif; ?> 

        </li> 

       <?php endwhile; endif; ?> 

       </ul>