I'm trying to display latest posts from multiple categories in the page by using foreach within javascript loop, but instead one by one the foreach display them all as in the picture below:
I want to display it one every element, not all of them in every element.
for (var i in books) { //Start JS Code
var html = '<div class="row"><div class="book_history">';
// JS Output
<?php // Start PHP
$catids = array(39, 37, 2); // Category for test
foreach ( $catids as $catid ) {
$my_query = new WP_Query( array(
'cat' => $catid,
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'no_found_rows' => true )
);
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
html += '<a href="<?php the_permalink(); ?>">»Chapter <?php the_title(); ?></a></div>'; // Javascript Code, Displaying the result of foreach with js
<?php endwhile;
endif;
wp_reset_postdata();
} ?> // End PHP
$bookcontainer.append(html); // Insert all result of foreach and js into some element
} // End JS Code
Is there any solution for this? *The order of categories with JavaScript is same.