I am trying to output a custom loop created using shortcode in a custom page template. Here is the code I am using to generate the shortcode:
function archive_sc( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'cat' => '',
'number' => 10,
), $atts )
);
$args = array(
'post_type' => 'smr-product',
'smr-product-category' => $cat, // 'taxonomy' => 'term',
'posts_per_page' => $number,
);
// The Query
$the_query = null;
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
smr_product_the_frontend_item();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'pcats', 'archive_sc' );
And, here is the code in Custom Page Template:
<?php
/*
Template Name: Product Archive
*/
get_header();
?>
<section id="content" class="review-container">
<?php while ( have_posts() ) : the_post(); ?>
<div id="list-items-review">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</section>
<?php get_footer(); ?>
The smr_product_the_frontend_item()
is just a function for the HTML of each product.
However, its returning infinite number of posts (although the blog has roughly 20 products) from nowhere. What I am doing wrong?
The problem seems to be in the Loop. Inside the Loop you are not incrementing the
$the_query
. You may change the code for the Loop as below: