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:
- add_filter(‘wp_enqueue_scripts’, function(){…}, 12);
- This line registers a custom function to the wp_enqueue_scripts filter, which controls the enqueuing and dequeueing of scripts and styles in WordPress.
- The 12 priority ensures this function runs after the default priority (10), allowing other scripts to be enqueued first.
- if( is_single() && ‘listing’ == get_post_type() )
- This if statement checks two conditions:
- is_single(): Confirms that the current page is a single post page.
- ‘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.
- This if statement checks two conditions:
- wp_dequeue_script(‘tdb_js_posts_autoload’);
- This function removes (or dequeues) a JavaScript file with the handle tdb_js_posts_autoload, which is likely responsible for the infinite scrolling feature.
- 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.