I'm using a shortcode to display a custom post type. The shortcode contains an attribute to display only posts of a specific category within the custom post type e.g. [faq category="travel"]
For each shortcode wordpress outputs a specific set of html, including a div with id of "faq-accordion". For each shortcode I need to output the div with ID "faq-accordion+1" e.g "faq-accordion1", "faq-accordion2", "faq-accordion3", etc.
I have managed to append a number to each div but for some reason it only goes as far as 6 and then repeats the numbers.
Here is my code
add_shortcode( 'faq', 'wpse105856_shortcode_callback' );
function wpse105856_shortcode_callback( $atts ) {
extract( shortcode_atts( array(
'category' => ''
), $atts ) );
$args = array(
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'faq'
);
if ( ! empty( $category ) ) {
$args['category_name'] = $category;
};
$posts = get_posts( $args );
//Here is where I am trying to dynamically add a new number to the faq-accordion div
$num = 0;
foreach ( $posts as $post ) {
$faq = '<div id="faq-accordion'.++$num.'">'; //Open the container
};
foreach ( $posts as $post ) { // Generate the markup for each Question
$faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
$post->post_title,
wpautop($post->post_content)
);
};
$faq .= '</div>'; //Close the container
return $faq; //Return the HTML.
};