- This topic has 6 replies, 3 voices, and was last updated 2 years, 7 months ago by Tom.
-
AuthorPosts
-
September 17, 2021 at 12:46 am #1933406Sunny
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).
September 17, 2021 at 1:23 am #1933426ElvinStaffCustomer SupportHi 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
September 17, 2021 at 1:30 am #1933432SunnyHey @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…
September 17, 2021 at 1:33 am #1933436ElvinStaffCustomer SupportI 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 thisif ( 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; } );
September 17, 2021 at 1:49 am #1933451SunnyI 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:
September 17, 2021 at 10:36 am #1934014SunnyHey @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.
September 17, 2021 at 8:38 pm #1934338TomLead DeveloperLead DeveloperHi 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
and456
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!
-
AuthorPosts
- You must be logged in to reply to this topic.