2015-09-28 53 views
-1

我在運行我的functions.php文件裏面的查詢時遇到了問題。我有一個相當廣泛的函數,它根據許多設置參數返回一組帖子。

我現在需要在函數循環內部運行一個循環來給我另一個結果。

如果我這樣做在一個正常的頁面看起來這和正常工作:如果我想然而

<div class="brief"> 
    <a href="<?php the_permalink(); ?>"><?php the_title() ?></a>  
    <?php 

     $terms = get_the_terms($post->ID, 'supplier-tax'); 
     foreach ($terms as $term) { 
      $termID[] = $term->term_id; 
     } 

     $the_query = new WP_Query(array(
     'post_type' => 'supplier', 
     'tax_query' => array(
      array(  
       'taxonomy' => 'supplier-tax', 
       'terms' => $termID, 
      ) 
      ), 
     )); 

     while ($the_query->have_posts()) : $the_query->the_post(); ?> 

     <p class="suppy"><?php the_title(); ?></p> 

     <?php endwhile; ?> 
     <?php wp_reset_postdata(); ?> 

    <p class="hide-for-small-only"><?php echo get_the_excerpt(); ?></p> 
    <a class="more-btn" href="<?php the_permalink(); ?>">More</a> 
</div>     
</div> 

輸出這在我的功能$out部分,這是行不通的,這是我的最新嘗試:

$out .= '<div class="small-12 large-6 columns end thumb under" data-equalizer-watch> 
      <div id="case"> 

       <div class="th" id="element" style="background-image: url('.get_field("product_image", false, true).')"> 
        <a class="fill-div" href="'. get_permalink($post->ID) .'"></a> 
       </div> 
      </div> 


      <div class="brief"> 
       <a href="'. get_permalink($post->ID) .'">'.get_the_title().'</a>  


       '$terms = get_the_terms($post->ID, 'supplier-tax'); 
       foreach ($terms as $term) { 
        $termID[] = $term->term_id; 
       } 

       $the_query = new WP_Query(array(
       'post_type' => 'supplier', 
       'tax_query' => array(
        array(  
         'taxonomy' => 'supplier-tax', 
         'terms' => $termID, 
        ) 
        ), 
       )); 

       while ($the_query->have_posts()) : $the_query->the_post();' 

       <p class="suppy">'.get_the_title().'</p> 

       'endwhile;' 
       'wp_reset_postdata();' 

       <p class="hide-for-small-only">'.get_the_excerpt().'</p> 
       <a class="more-btn" href="'. get_permalink($post->ID) .'">More</a> 
      </div>     
     </div>'; 

根據調試我已經unxepected $terms這是該代碼的第9行查詢的第一線,但我不知道我需要怎麼回事,添加$術語誰能幫助?

+1

嚴肅地說,我建議實際上退後一步並學習如何在直接嘗試寫入WordPress的東西之前編寫PHP。 –

+0

分開你的PHP變量這持有HTML,並保持你的循環/ PHP代碼的變量。 – Noman

+0

@JonStirling它的寫法有什麼問題? – Addzy

回答

0

你需要的是創建單獨的變量。和你使用它。在啓動變量之前準備參數terms

試試這個。

$backgroundImg = get_field("product_image", false, true); 
$permalink = get_permalink($post->ID); 
$postTitle = get_the_title($post->ID); 
$postExcerpt = get_the_excerpt($post->ID); 

$terms = get_the_terms($post->ID, 'supplier-tax'); 
foreach ($terms as $term) { 
    $termID[] = $term->term_id; 
} 

$the_query = new WP_Query(array(
    'post_type' => 'supplier', 
    'tax_query' => array(
     array(
      'taxonomy' => 'supplier-tax', 
      'terms' => $termID, 
     ) 
    ), 
)); 

$out = '<div class="small-12 large-6 columns end thumb under" data-equalizer-watch> 
      <div id="case"> 

       <div class="th" id="element" style="background-image: url('.$backgroundImg.')"> 
        <a class="fill-div" href="'. $permalink .'"></a> 
       </div> 
      </div> 

      <div class="brief"> 
       <a href="'. $permalink .'">'.$postTitle.'</a>'; 

       while ($the_query->have_posts()) : $the_query->the_post(); 
        $out .= '<p class="suppy">'.get_the_title().'</p>'; 
       endwhile; 

       wp_reset_postdata(); 

       $out .= '<p class="hide-for-small-only">'.$postExcerpt.'</p> 
       <a class="more-btn" href="'. $permalink .'">More</a> 
      </div> 
     </div>'; 

echo $out;