Site logo

Archived topic

Display custom template for CPT only if there is data to loop on

3 replies · Started by Thibaud on March 4, 2022

Viewing posts 1–4 of 4

Hi guys !

I have build a custom template inside the GeneratePress Builder for one of my CPT.
I use "Dynamic Content" and "Dynamic Image" blocks, but when I have no data (CPT) to loop on, the box is show one time with empty data inside it.

Is there a possibility to display this custom template only if I have data to loop on ? (like en for-else loop to speak dev)

Thank you for your help !
Thibaud

Hi there,

we had one other reported issue of this, see Tom's temporary solution for that here:

https://generatepress.com/forums/topic/posts-page-as-company-job-postings/page/2/#post-2118759

To use that try the following snippet:

add_filter( 'generate_element_display', function( $display, $element_id ) {

    if ( 675 === $element_id && ! have_posts() ) {
        $display = false;
    }

    return $display;
}, 10, 2 );

Where the 675 is you need to replace that with the ID of the element.

Hi David,

Sorry I couldn't find this issue in my researches.
Perfect, it works thanks !

Its only come up once :) And it took me a while to find it ( well i asked Tom who found it lol ).

Glad to be of help. Its something we will fix in the future.

This archived topic is closed to new replies.