Site logo

[Resolved] No posts template on custom post type archive

Home Forums Support [Resolved] No posts template on custom post type archive

Home Forums Support No posts template on custom post type archive

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1996780
    Markus

    Hi, I’ve created a custom post type (vacant positions) and created an archive page and a single page for the post type using Block elements – Content template. This works great. Coming from a developer background were I used to do this “the old” way with template files, this really is a smooth way of handling it. I’m using the archive page to show all the vacant positions, using a Block – page hero and a Block – content element. Is there a way to show a different Block element or adjust the current Block element if there are no vacant positions to show, without using code? For example, just showing “Currently there are no vacant positions”.

    #1996964
    David
    Staff
    Customer Support

    Hi there,

    Thats great to hear – thanks for the feedback!!

    at the moment there are minimal options for conditionally displaying the block content. Container Blocks in the Block Element do have: Remove container condition in sidebar settings. But it’s limited to No Featured Image or a Post Meta Value.

    Which leaves us with the code alternative using then generate_element_display filter to swap the Element:

    https://docs.generatepress.com/article/generate_element_display/

    #1997063
    Markus

    Thanks for quick respons! Ok, so one filter for showing the default element and one filter for showing the “no post” element? Got this to work. Do you se any improvements that could be done?

    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 721 === $element_id && is_post_type_archive('vacant-positions') ) {
            if ( $post ){
                $display = true;
            } else {
                $display = false;
            }
        }
        return $display;
    }, 10, 2 );
    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 754 === $element_id && is_post_type_archive('vacant-positions') ) {
            if ( !$post ){
                $display = true;
            } else {
                $display = false;
            }
        }
        return $display;
    }, 10, 2 );
    
    #1997071
    David
    Staff
    Customer Support

    Thats the way i would do it!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.