Site logo

[Resolved] How can I customize the "Nothing Found" layout on archive.php with Elements?

Home Forums Support [Resolved] How can I customize the "Nothing Found" layout on archive.php with Elements?

Home Forums Support How can I customize the "Nothing Found" layout on archive.php with Elements?

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #729828
    Pete

    How can I customize the “Nothing Found” layout on archive.php with Elements?

    #729961
    Leo
    Staff
    Customer Support

    Hi there,

    What are you looking to customize?

    It should be the 404 template under Display Rule.

    Let me know 🙂

    #729965
    Pete

    Nothing Found isn’t the same as the 404 page. Nothing Found appears when the URL is correct but there’s no posts in that category yet.

    #729985
    Tom
    Lead Developer
    Lead Developer

    Currently, there are no Display Rules for that, as there isn’t a condition for it inside WP.

    We might be able to use a filter to display a certain element if there are no search results though. Would that work for you?

    #729987
    Pete

    That sounds worth a go, thanks.

    #730008
    Tom
    Lead Developer
    Lead Developer

    So first, create a new Element and take note of its ID.

    Then you can use the filter (dependent on the kind of Element):
    https://docs.generatepress.com/article/generate_layout_element_display/
    https://docs.generatepress.com/article/generate_header_element_display/
    https://docs.generatepress.com/article/generate_hook_element_display/

    So for example, if we’re doing a Header Element:

    add_filter( 'generate_header_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id && is_search() ) {
            global $wp_query;
    
            if ( ! $wp_query->found_posts ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );

    The only thing you’d need to change is the element ID (10).

    #730012
    Pete

    That’s great thanks Tom I’ll give it a try and get back to you.

    #730639
    Pete

    OK, I have this in a hook element…

    add_filter( 'generate_header_element_display', function( $display, $element_id ) {
        if ( 80 === $element_id) {
            global $wp_query;
    
            if ( ! $wp_query->found_posts ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );

    Hook: before_main_content
    Display rule: Post category

    What next?

    #731131
    Tom
    Lead Developer
    Lead Developer

    What that code does is set it so that Element (80) should display on the no results page.

    You should add it using one of these methods: https://docs.generatepress.com/article/adding-php/

    #737261
    Pete

    I’m not following exactly what to do.

    #737559
    Tom
    Lead Developer
    Lead Developer

    1. Create your Element, as you would if you could choose the “Nothing Found” page in Display Rules. Take note of the Element ID once you publish it. Leave Display Rules empty.

    2. Add the above function using one of these methods: https://docs.generatepress.com/article/adding-php/

    In the code, replace 10 with the ID of the Element you published in step 1.

    Now the Element you published will appear on the Nothing Found template.

    #737646
    Pete

    1. Create your Element,

    A hook element?

    #737957
    Tom
    Lead Developer
    Lead Developer

    It depends on what you’re trying to add to the No Results page. What exactly are you wanting to change on that page?

    #738026
    Pete

    Just the text to say something else.

    #738664
    Tom
    Lead Developer
    Lead Developer

    Currently, the text for the no results page isn’t filterable.

    So, you’d need to copy this file: https://github.com/tomusborne/generatepress/blob/2.2.1/no-results.php

    And add it to your child theme. Make sure it keeps the same filename: no-results.php

    Then you could change the text to whatever you like.

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