2015-09-28 54 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;