2013-12-17 55 views
0

我试图通过简码输出与自定义帖子类型相关联的所有标签,并且它只显示在$输出内引入1个标签。Wordpress在循环中输出标签

$输出之外的代码是好的。

代码:

function display_stores() { 

    $args = array('post_type' => 'stores', 'posts_per_page' => 5); 

    $success = new WP_Query($args); 

     $output = ''; 

     while($success->have_posts()) { 

      $success->the_post(); 

      $tags = get_the_tags($post_ID); 

      foreach($tags as $tag) { 

       return '<li>'. $tag->name . '</li>' ; 


      } 

      $output .= sprintf("<div class='story-content left'>"); 
      $output .= sprintf("<h2>%s</h2>", get_the_title()); 
      $output .= sprintf('%s</div>', get_the_content()); 
      $output .= sprintf("Button"); 
      $output .= sprintf("<div class='story-tags right'>"); 
      $output .= sprintf("<h4>Areas</h4><ul class='ul-arrows'>"); 
      $output .= sprintf($tags); 
      $output .= sprintf("</ul></div><hr>"); 

     } 

      wp_reset_query(); 

      return $output; 

} 

add_shortcode('display_stores', 'display_stores'); 

回答

2
foreach($tags as $tag) { 
    return '<li>'. $tag->name . '</li>' ; 
} 

第一次,这是跑,将退出功能,并返回li。我想你打算将它添加到输出。

$tagHTML = ''; 
foreach($tags as $tag) { 
    $tagHTML .= '<li>'. $tag->name . '</li>' ; 
} 
//Later 
$output .= $tagHTML; 
+0

谢谢 - 完美的工作! – jolen

+0

@ jolen没问题。很高兴我能帮上忙。 – Jim