How to disable continuous/infinite scroll on specific condition in TagDiv’s Newspaper Theme

Facebook
Twitter
LinkedIn

The Newspaper theme by TagDiv is an excellent choice for publishers, though it provides limited controls for managing continuous or infinite scrolling. In this example, we’ll focus on dequeueing the JavaScript responsible for infinite scrolling on single pages within a specific custom post type.

<?php

add_filter('wp_enqueue_scripts', function(){
	if( is_single() && 'listing' == get_post_type()  ) {
		wp_dequeue_script('tdb_js_posts_autoload');
	}
},12);

Explained

This PHP code snippet customizes the behavior of the WordPress theme by conditionally dequeueing a specific JavaScript file related to infinite scrolling on certain pages. Here’s a breakdown of the code:

  1. add_filter(‘wp_enqueue_scripts’, function(){…}, 12);
    1. This line registers a custom function to the wp_enqueue_scripts filter, which controls the enqueuing and dequeueing of scripts and styles in WordPress.
    2. The 12 priority ensures this function runs after the default priority (10), allowing other scripts to be enqueued first.
  2. if( is_single() && ‘listing’ == get_post_type() )
    1. This if statement checks two conditions:
      1. is_single(): Confirms that the current page is a single post page.
      2. ‘listing’ == get_post_type(): Ensures the post type is specifically “listing.” This means the code will only affect single pages for posts of this type.
  3. wp_dequeue_script(‘tdb_js_posts_autoload’);
    1. This function removes (or dequeues) a JavaScript file with the handle tdb_js_posts_autoload, which is likely responsible for the infinite scrolling feature.
  4. By removing this script, infinite scrolling is disabled on the specified post type (“listing”).

Summary

This code disables the infinite scrolling JavaScript (tdb_js_posts_autoload) on single pages for posts of the “listing” custom post type. It provides more control over where infinite scrolling is active by selectively dequeueing the script.

Matt Pramschufer is a seasoned technology expert and co-founder of E-Moxie, where he specializes in transforming visionary ideas and business goals into thriving online ventures. With over two decades of experience in website design, development, and custom application creation, Matt understands that success requires more than just a visually appealing website or a well-built app. He combines proven methodologies with the latest internet technologies to deliver digital solutions that not only look great but also drive measurable growth.