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?

2
  • Where are you calling the shortcode? The custom template just looks like the standard loop. Mar 19, 2014 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, 2014 at 16:10

2 Answers 2

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
}
0

the issue might be related to how you are calling the smr_product_the_frontend_item() function within your loop. It's likely causing an infinite loop because you are not advancing the loop using the_post()

Shortcode function

function archive_sc( $atts ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'cat' => '',
            'number' => 10,
        ),
        $atts
    );

    $args = array( 
        'post_type' => 'smr-product',
        'smr-product-category' => $atts['cat'], // 'taxonomy' => 'term',
        'posts_per_page' => $atts['number'],
    );

    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    ob_start();
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            smr_product_the_frontend_item();
        }
    } else {
        // no posts found
    }
    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode( 'pcats', 'archive_sc' );

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 echo do_shortcode(get_the_content()); ?>
        </div>
    <?php endwhile; ?>  
    </section>


<?php get_footer(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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