Archived topic
How can I customize the "Nothing Found" layout on archive.php with Elements?
14 replies · Started by Pete on November 17, 2018
How can I customize the "Nothing Found" layout on archive.php with Elements?
Hi there,
What are you looking to customize?
It should be the 404 template under Display Rule.
Let me know :)
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.
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?
That sounds worth a go, thanks.
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).
That's great thanks Tom I'll give it a try and get back to you.
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?
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/
I'm not following exactly what to do.
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.
1. Create your Element,
A hook element?
It depends on what you're trying to add to the No Results page. What exactly are you wanting to change on that page?
Just the text to say something else.
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.