Site logo

Archived topic

Removing Default Loop from Pages

6 replies · Started by Sunny on September 17, 2021

Viewing posts 1–7 of 7

Hey Team,

I've created a website using the GeneratePress theme and added a custom query in the elements to show posts from a few categories (and disabled the GeneratePress default loop) using the code shared below (please see "Link 1" in private information for this reference).

add_filter( 'generate_has_default_loop', function( $show ) {
    if ( is_home() ) {
        return false;
    }
	else {
		return true;
	}
    
} );

By adding this code I stopped the default loop of GeneratePress and created my own "Posts Section" to show data within.

Using the same procedure, I can show posts from one category only on the "blog" page (Check "Link 2" in your private information) by editing the WP_query in the elements of the "blog" page to this:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
    'post_type'      => 'post', // You can add a custom post type if you like
    'paged'          => $paged,
    'category_name' => 'blog',
    'posts_per_page' => 4
));
?>

And disabling the generatepress default loop using the code shared below,

add_filter( 'generate_has_default_loop', function( $show ) {
    if ( is_page('blog') ) {
        return false;
    }
	else {
		return true;
	}
    
} );

After that, the posts will be loaded using...

<?php if ( have_posts() ) : ?>
					 <?php while ( have_posts() ) : the_post();  ?>

This successfully shows the post from that category, however, it also loads the default generate press loop (which I want to disable) (Check "Image Link" in private information for reference).

Hi there,

Have you sorted this out? I've checked the link w/ the /blog and I don't see the duplicate loop you've indicated on the screenshot.

Here's what I see on my end - https://share.getcloudapp.com/P8uGrLJ6

Hey @Elvin,

Due to not clearing the cache, it does not appear to have duplicate loops on your end.

It was cleared just now, please check it...

I see.

I think the issue has something to do with your filter's condition.

What happens if you change this line if ( is_page('blog') ) to this if ( is_page(543) )

If this doesn't work:

do you have multiple instances of the same filter? that may cause conflict.

If you do have multiple instances of the same filter, we can merge the two so you only need to maintain 1.

Example:
Merging your 2 codes.

add_filter( 'generate_has_default_loop', function( $show ) {
    if ( is_home() || is_page(543) ) {
        $show = false;
    }
	else {
		$show = true;
	}
    return $show;
} );

I tried both of the methods you shared above, but they aren't working for me. Even after merging them as follows, I still can't get it to work:

add_filter( 'generate_has_default_loop', function( $show ) {
    if ( is_home() ) {
        return false;
    }
	elseif ( is_page('blog') ) {
        return false;
    }
	else {
		return true;
	}
    
} );

I am also sharing a temporary access to the website in private information, please take a look:

Hey @elvin

I think this file loads because of the "inc/content-page.php" file inside of the GeneratePress theme. Please have a look and let me know the ways to solve this issue.

Hi there,

Your code should look something like this:

add_filter( 'generate_has_default_loop', function( $show ) {
    $pages_to_disable = array( 123, 456 );

    if ( is_home() || is_page( $pages_to_disable ) ) {
        return false;
    }

    return $show;
} );

You just need to replace 123 and 456 with the IDs of the pages you want to disable the loop on. If you only want one page, you can change it to:

$pages_to_disable = array( 123 );

Hope this helps!

This archived topic is closed to new replies.