2016-12-26 106 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>