1

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?

  • Where are you calling the shortcode? The custom template just looks like the standard loop. – Andrew Bartel Mar 19 '14 at 16:08
  • @AndrewBartel Yes, it's a standard loop. I am calling the custom loop inside of the page content using shortcode. – Abhik Mar 19 '14 at 16:10
0

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:

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();  // Add this line to fix the issue
        smr_product_the_frontend_item();
    }
} else {
    // no posts found
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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