Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a shortcode for a custom post type so I can show the posts in a page. It works, except it only shows 1 post.

here the code... anyone know what's wrong?

// 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');
}
?>

Here is the new updated code

// 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');
}
?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You're only seeing one post because with each iteration of the loop you're overwriting the previous iteration's value of $ea_var on the line $ea_var = '<article id="post-';. declare $ea_var outside the while loop and change that line to append with .=

One other issue you've got is the use of $post->ID won't work unless you first globalize $post.

I also suggest you use a new instance of WP_Query instead of query_posts, and then just wp_reset_postdata() after the loop, otherwise the shortcode will have unexpected results in some cases.

share|improve this answer
    
Great thanks. Will give it a try –  greg Sep 19 '12 at 21:28
    
sorry, typo above, I meant wp_reset_postdata –  Milo Sep 19 '12 at 21:31
    
How would I go about declaring the variable outside the loop? I mean what would the variable equal? (btw. I changed the $ea_var = '<article id="post-'; to $ea_var .= '<article id="post-'; and everything started working properly. –  greg Sep 19 '12 at 21:51
    
that works, though it gives a php undefined var error notice. just do $ea_var = '' before the while loop. –  Milo Sep 19 '12 at 21:55
    
for instance, add $ea_var = ''; before the 'query_posts()' line. –  Michael Sep 19 '12 at 21:57
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.