Custom Loop Using Shortcode in Custom Page Template

admin2025-01-07  4

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?

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?

Share Improve this question asked Mar 19, 2014 at 15:56 AbhikAbhik 2,9212 gold badges23 silver badges31 bronze badges 2
  • Where are you calling the shortcode? The custom template just looks like the standard loop. – Andrew Bartel Commented 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 Commented Mar 19, 2014 at 16:10
Add a comment  | 

2 Answers 2

Reset to default 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
}

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(); ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260112a640.html

最新回复(0)