为什么我的自定义POST查询快捷代码只显示1个POST?

时间:2012-09-19 作者:greg

我为自定义帖子类型创建了一个快捷码,以便在页面中显示帖子。它可以工作,只是它只显示1个帖子。

这里是代码。。。有人知道怎么了吗?

// this is the shortcode function which includes the query 
function ea_get_location($atts){
   extract(shortcode_atts(array(
      \'destination\' => null,
      \'posts\' => \'10\'

   ), $atts));

$ea_params = array(
  \'post_type\'=>\'itineraries\',
  \'posts_per_page\' => $posts,
  \'tax_query\' => array(
    array(
      \'taxonomy\' => \'location\',
      \'field\' => \'slug\',
      \'terms\' => $destination
      ))
  );

query_posts( $ea_params );
      while (have_posts()) : the_post();

$ea_var = \'<article id="post-\';
$ea_var .= get_the_ID();
$ea_var .= \'" class="post-\';
$ea_var .= get_the_ID();
$ea_var .= \' itineraries type-itineraries hentry">\';
$ea_var .= \'<header class="entry-header">\';
$ea_var .= \'<a href="\';
$ea_var .= get_permalink();
$ea_var .= \'#access" >\';
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'itinerary-thumb\' );
$thumb_url = $thumb[\'0\'];
$ea_var .= \'<img src="\'.$thumb_url.\'" class="attachment-itinerary-thumb wp-post-image" />\';
$ea_var .= \'</a><h2 class="entry-title">\';
$ea_var .= \'<a href="\';
$ea_var .= get_permalink();
$ea_var .= \'#access">\'; 
$ea_var .= get_the_title();
$ea_var .= \'</a></h2></header><div class="entry-summary">\';
$ea_var .= get_the_excerpt();
$ea_var .= \'</div></article>\';   

    endwhile;    
// Reset Query
wp_reset_query();
// return the variable for use in the shortcode
return \'<div id="location-list" class="tax-location">\'.$ea_var.\'</div>\';
}

// register the function as a shortcode
function register_shortcodes(){
   add_shortcode(\'location\', \'ea_get_location\');
}
?>
这是新的更新代码

// this is the shortcode function which includes the query 
function ea_get_location($atts){
   extract(shortcode_atts(array(
      \'destination\' => \'\',
      \'posts\' => -1

   ), $atts));

$ea_params = array(
  \'post_type\'=>\'itineraries\',
  \'posts_per_page\' => $posts,
  \'tax_query\' => array(
    array(
      \'taxonomy\' => \'location\',
      \'field\' => \'slug\',
      \'terms\' => $destination
      ))
  );

// The Query
$ea_itin_query = new WP_Query( $ea_params );

$ea_var = \'\';

while ($ea_itin_query->have_posts()) : $ea_itin_query->the_post();

global $post;

$ea_var .= \'<article id="post-\';
$ea_var .= get_the_ID();
$ea_var .= \'" class="post-\';
$ea_var .= get_the_ID();
$ea_var .= \' itineraries type-itineraries hentry">\';
$ea_var .= \'<header class="entry-header">\';
$ea_var .= \'<a href="\';
$ea_var .= get_permalink();
$ea_var .= \'#access" >\';
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'itinerary-thumb\' );
$thumb_url = $thumb[\'0\'];
$ea_var .= \'<img src="\'.$thumb_url.\'" class="attachment-itinerary-thumb wp-post-image" />\';
$ea_var .= \'</a><h2 class="entry-title">\';
$ea_var .= \'<a href="\';
$ea_var .= get_permalink();
$ea_var .= \'#access">\'; 
$ea_var .= get_the_title();
$ea_var .= \'</a></h2></header><div class="entry-summary">\';
$ea_var .= get_the_excerpt();
$ea_var .= \'</div></article>\';   

endwhile;

// Reset Post Data
wp_reset_postdata();

// return the variable for use in the shortcode
return \'<div id="location-list" class="tax-location">\'.$ea_var.\'</div>\';
}

// register the function as a shortcode
function register_shortcodes(){
   add_shortcode(\'location\', \'ea_get_location\');
}
?>

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您只看到一篇帖子,因为在循环的每次迭代中,您都会覆盖上一次迭代的值$ea_var 在线上$ea_var = \'<article id="post-\';. 声明$ea_var 在while循环外部,并将该行更改为附加.=

另一个问题是$post->ID 除非你先全球化否则不会成功$post.

我还建议您使用WP_Query 而不是query_posts, 然后wp_reset_postdata() 在循环之后,否则在某些情况下,短代码将产生意外的结果。

结束

相关推荐

Shortcode Strategy

我希望将另一个数据库中的数据插入到我的帖子中,并考虑使用短代码。示例帖子:[imdb id=\"123\" name] was born [imdb id=\"123\" birthday] in [imdb id=\"123\" birth_location]. 我想缓存结果,一旦检索到,就不需要再运行短代码操作了。有没有什么好的策略可以让这种事情更有效率?