• I’m trying to get this to work. The code below sort of works. When the first instance of infinite scroll loads the offset works like a charm and the next 8 posts load exactly as expected. The problem is that every instance thereafter reloads the same 8 posts over and over again.

    This is building off of the default Jetpack setup that is included with a generated version of the _s starter theme (http://underscores.me/)

    /**
     * Jetpack setup function.
     *
     * See: https://jetpack.me/support/infinite-scroll/
     * See: https://jetpack.me/support/responsive-videos/
     */
    function wg_jetpack_setup() {
    	// Add theme support for Infinite Scroll.
    	add_theme_support( 'infinite-scroll', array(
    		'container' => array( 'content' ),
    		'render'    => 'wg_infinite_scroll_render',
    		'footer'    => 'page',
    		'posts_per_page' => 8,
    	) );
    
    	// Add theme support for Responsive Videos.
    	add_theme_support( 'jetpack-responsive-videos' );
    } // end function wg_jetpack_setup
    add_action( 'after_setup_theme', 'wg_jetpack_setup' );
    
    function jetpack_infinite_scroll_query_args( $args ) {
    	$args['post_type'] = 'product';
    	$args['offset'] = 8;
    
    	return $args;
    }
    add_filter( 'infinite_scroll_query_args', 'jetpack_infinite_scroll_query_args' );
    
    /**
     * Custom render function for Infinite Scroll.
     */
    function wg_infinite_scroll_render() {
    
    	while ( have_posts() ) {
    		the_post();
    		if ( is_search() ) {
    		    get_template_part( 'template-parts/content', 'search' );
    		} elseif (is_woocommerce() ) {
    		    wc_get_template_part( 'content', 'product' );
    		} else {
    			get_template_part( 'template-parts/content', get_post_format() );
    		}
    	}
    } // end function wg_infinite_scroll_render

    Any help would be appreciated. I’ve done some searching around and came across these threads:
    https://wordpress.org/support/topic/duplicate-posts-when-using-infinite-loop-and-offset

    https://wordpress.org/support/topic/cant-get-infinite-scroll-to-work

    They didn’t quite have solutions that worked for me. (Also likely that i just am not understanding how those solutions apply to my issue).

    Again any help is really appreciated.

    https://wordpress.org/plugins/jetpack/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    'container' => array( 'content' ),

    Could you try using a string ('container' => 'content',) instead of an array?

    `function jetpack_infinite_scroll_query_args( $args ) {
    $args[‘post_type’] = ‘product’;
    $args[‘offset’] = 8;

    return $args;
    }
    add_filter( ‘infinite_scroll_query_args’, ‘jetpack_infinite_scroll_query_args’ );`

    I’m not sure I understand the need for this code. Could you try removing it, or explaining what you’re trying to achieve? If you’d like to display 8 posts per page, you do not need that code. You also don’t need to specify a post type, as your loop should take care of that.

    is_woocommerce()

    It might be worth trying to use is_shop() instead, as I assume you’ll be using Infinite Scroll on your page listing all products.

    Let me know how it goes.

    Thread Starter bencross02

    (@bencross02)

    Thanks for the reply Jeremy. I tried the suggestions you mentioned but to no avail.

    I am not using the traditional “shop” page at all, instead using the permalink shop base option. http://www.weidmangallery.com is the website. When I switched to “is_shop()” it stops loading the woocommerce template.

    Currently I am using the Infinite Scroll plugin but since it is not being actively developed I figured it was probably a better idea to use the built in features of Jetpack.

    With all that said here is a the code that I am now using.

    /**
     * Jetpack setup function.
     *
     * See: https://jetpack.me/support/infinite-scroll/
     * See: https://jetpack.me/support/responsive-videos/
     */
    function wg_jetpack_setup() {
    	// Add theme support for Infinite Scroll.
    	add_theme_support( 'infinite-scroll', array(
    		'container' => 'content',
    		'render'    => 'wg_infinite_scroll_render',
    		'footer'    => 'page',
    		'posts_per_page' => 8,
    	) );
    
    	// Add theme support for Responsive Videos.
    	add_theme_support( 'jetpack-responsive-videos' );
    } // end function wg_jetpack_setup
    add_action( 'after_setup_theme', 'wg_jetpack_setup' );
    
    /**
     * Custom render function for Infinite Scroll.
     */
    function wg_infinite_scroll_render() {
    	while ( have_posts() ) {
    		the_post();
    		if ( is_search() ) {
    		    get_template_part( 'template-parts/content', 'search' );
    		} elseif ( is_woocommerce() ) {
    		    wc_get_template_part( 'content', 'product' );
    		} else {
    			get_template_part( 'template-parts/content', get_post_format() );
    		}
    	}
    } // end function wg_infinite_scroll_render

    This causes the first set of products to be duplicated. See screenshot here: http://www.weidmangallery.com/screenshot.png

    the second row is loaded by the Jetpack Infinite Scroll. That’s why I was trying to offset the posts. I just don’t know how to add that to the custom render function that’s calling the different templates.

    Any suggestions?

    Thanks again for your help.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Could you let me know what you have in your content-product.php file?

    Thread Starter bencross02

    (@bencross02)

    I’m just using the default WooCommerce content-product.php file.

    The only change I made was to the ‘loop_shop_columns’ part under “// Store column count for displaying the grid”. I made it 8 instead of 4 so I could show the repeating content together in one screen shot.

    Thanks again!

    <?php
    /**
     * The template for displaying product content within loops.
     *
     * Override this template by copying it to yourtheme/woocommerce/content-product.php
     *
     * @author  WooThemes
     * @package WooCommerce/Templates
     * @version 2.4.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    global $product, $woocommerce_loop;
    
    // Store loop count we're currently on
    if ( empty( $woocommerce_loop['loop'] ) ) {
    	$woocommerce_loop['loop'] = 0;
    }
    
    // Store column count for displaying the grid
    if ( empty( $woocommerce_loop['columns'] ) ) {
    	$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 8 );
    }
    
    // Ensure visibility
    if ( ! $product || ! $product->is_visible() ) {
    	return;
    }
    
    // Increase loop count
    $woocommerce_loop['loop']++;
    
    // Extra post classes
    $classes = array();
    if ( 0 == ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns'] ) {
    	$classes[] = 'first';
    }
    if ( 0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) {
    	$classes[] = 'last';
    }
    ?>
    <li <?php post_class( $classes ); ?>>
    
    	<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
    
    	<a href="<?php the_permalink(); ?>">
    
    		<?php
    			/**
    			 * woocommerce_before_shop_loop_item_title hook
    			 *
    			 * @hooked woocommerce_show_product_loop_sale_flash - 10
    			 * @hooked woocommerce_template_loop_product_thumbnail - 10
    			 */
    			do_action( 'woocommerce_before_shop_loop_item_title' );
    
    			/**
    			 * woocommerce_shop_loop_item_title hook
    			 *
    			 * @hooked woocommerce_template_loop_product_title - 10
    			 */
    			do_action( 'woocommerce_shop_loop_item_title' );
    
    			/**
    			 * woocommerce_after_shop_loop_item_title hook
    			 *
    			 * @hooked woocommerce_template_loop_rating - 5
    			 * @hooked woocommerce_template_loop_price - 10
    			 */
    			do_action( 'woocommerce_after_shop_loop_item_title' );
    		?>
    
    	</a>
    
    	<?php
    
    		/**
    		 * woocommerce_after_shop_loop_item hook
    		 *
    		 * @hooked woocommerce_template_loop_add_to_cart - 10
    		 */
    		do_action( 'woocommerce_after_shop_loop_item' );
    
    	?>
    
    </li>
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Thanks for the extra details!

    Do you think you could contact us via email, so we can run some more tests with you?
    http://jetpack.me/contact-support/

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Infinite Scroll with WooCommerce – duplicate content when using offset’ is closed to new replies.