The code below is meant to simply loop through posts, give the opportunity to add html formatting to them, then return them to the shortcode position. I have conditional logic which defines formatting based on the passed in "category"(term). I need to output it twice on the same page, but I only see code that I've added outside of the loop.
add_shortcode('my_shortcode','generate_my_shortcode_content');
function generate_my_shortcode_content($atts){
$a = shortcode_atts( array(
'post-count' => 3,
'category' => 'default-cat'
), $atts );
$post_count = (int)$a['post-count'];
$post_category = explode(',', $a['category']);
$args = array(
'post_type' => 'my-type',
'posts_per_page' => $post_count,
'tax_query' => array(
array(
'taxonomy' => 'my-taxonomy',
'field' => 'slug',
'terms' => $post_category
)
)
);
$test = "<h1>";
$query = new WP_Query( $args );
if($query->have_posts()):while($query->have_posts()):$query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title($post_id);
$post_content = get_the_content($post_id);
$posts .= $post_id . $post_title . $post_content;
$test .= "this is a test";
endwhile;wp_reset_postdata();endif;
$test .= "</h1>";
return $posts . $test;
}
This function will return {Post ID}{Post Title}{Post Content}<h1>this is a test</h1>
on the first execution, but will only return <h1></h1>
on the second. Why is this?