-
AuthorPosts
-
May 6, 2020 at 11:36 pm #1210545
Hi,
Which function do I need to add to modify the text for search with no results? (see screenshot in private content)
I was told to edit the search.php file, but I would prefer to add a function in the functions.php file of my child theme, so it will be independent of any theme update.
Which code do i need to add?If needed, you can find a link and credentials to my website in private content.
Thanks a lot!
May 10, 2020 at 9:26 pm #1211703Hey fcp,
Try adding this code to the end of your functions.php file in Appearance > Editor:if( ! function_exists( 'avia_ajax_search' ) ) { //now hook into wordpress ajax function to catch any ajax requests add_action( 'wp_ajax_avia_ajax_search', 'avia_ajax_search' ); add_action( 'wp_ajax_nopriv_avia_ajax_search', 'avia_ajax_search' ); function avia_ajax_search() { unset( $_REQUEST['action'] ); if( empty( $_REQUEST['s'] ) ) { $_REQUEST['s'] = array_shift( array_values( $_REQUEST ) ); } if( empty( $_REQUEST['s'] ) ) { die(); } $defaults = array( 'numberposts' => 5, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false, 'results_hide_fields' => '' ); /** * WP Filter for the contents of the search query variable * * @param string * @return string */ $_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s'] ); $search_parameters = array_merge( $defaults, $_REQUEST ); if ( $search_parameters['results_hide_fields'] !== '' ) { $search_parameters['results_hide_fields'] = explode( ',', $_REQUEST['results_hide_fields'] ); } else { $search_parameters['results_hide_fields'] = array(); } /** * @used_by Avia_Custom_Pages 10 * @used_by config-woocommerce\config.php avia_woocommerce_ajax_search_params() 20 * * @param array * @return array */ $search_query = apply_filters( 'avf_ajax_search_query', http_build_query( $search_parameters ) ); /** * @used_by Avia_Relevanssi 10 * * @param string $function_name * @param array $search_query * @param array $search_parameters * @param array $defaults * @return string */ $query_function = apply_filters( 'avf_ajax_search_function', 'get_posts', $search_query, $search_parameters, $defaults ); $posts = ( ( $query_function == 'get_posts') || ! function_exists( $query_function ) ) ? get_posts( $search_query ) : $query_function( $search_query, $search_parameters, $defaults ); $search_messages = array( 'no_criteria_matched' => __( 'Sorry, no match', 'avia_framework' ), 'another_search_term' => __( 'Please try again', 'avia_framework' ), 'time_format' => get_option( 'date_format' ), 'all_results_query' => http_build_query( $_REQUEST ), 'all_results_link' => home_url( '?' . http_build_query( $_REQUEST ) ), 'view_all_results' => __( 'View all results', 'avia_framework' ) ); $search_messages = apply_filters( 'avf_ajax_search_messages', $search_messages, $search_query ); if( empty( $posts ) ) { $output = "<span class='av_ajax_search_entry ajax_not_found'>"; $output .= "<span class='av_ajax_search_image " . av_icon_string('info') . "'>"; $output .= '</span>'; $output .= "<span class='av_ajax_search_content'>"; $output .= "<span class='av_ajax_search_title'>"; $output .= $search_messages['no_criteria_matched']; $output .= '</span>'; $output .= "<span class='ajax_search_excerpt'>"; $output .= $search_messages['another_search_term']; $output .= '</span>'; $output .= '</span>'; $output .= '</span>'; echo $output; die(); } //if we got posts resort them by post type $output = ''; $sorted = array(); $post_type_obj = array(); foreach( $posts as $post ) { $sorted[ $post->post_type][] = $post; if( empty( $post_type_obj[ $post->post_type ] ) ) { $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type ); } } //now we got everything we need to preapre the output foreach( $sorted as $key => $post_type ) { // check if post titles are in the hidden fields list if ( ! in_array( 'post_titles', $search_parameters['results_hide_fields'] ) ) { if( isset( $post_type_obj[ $key ]->labels->name ) ) { $label = apply_filters( 'avf_ajax_search_label_names', $post_type_obj[ $key ]->labels->name ); $output .= "<h4>{$label}</h4>"; } else { $output .= '<hr />'; } } foreach( $post_type as $post ) { $image = ''; $extra_class = ''; // check if image is in the hidden fields list if ( ! in_array( 'image', $search_parameters['results_hide_fields'] ) ) { $image = get_the_post_thumbnail( $post->ID, 'thumbnail' ); $extra_class = $image ? 'with_image' : ''; $post_type = $image ? '' : ( get_post_format( $post->ID ) != '' ? get_post_format( $post->ID ) : 'standard' ); $iconfont = $image ? '' : av_icon_string( $post_type ); } $excerpt = ''; // check if post meta fields are in the hidden fields list if ( ! in_array( 'meta', $search_parameters['results_hide_fields'] ) ) { if( ! empty($post->post_excerpt ) ) { $excerpt = apply_filters( 'avf_ajax_search_excerpt', avia_backend_truncate( $post->post_excerpt, 70, ' ', '...', true, '', true ) ); } else { $excerpt = apply_filters( 'avf_ajax_search_no_excerpt', get_the_time( $search_messages['time_format'], $post->ID ), $post ); } } $link = apply_filters( 'av_custom_url', get_permalink( $post->ID ), $post ); $output .= "<a class ='av_ajax_search_entry {$extra_class}' href='{$link}'>"; if ( $image !== '' || $iconfont ) { $output .= "<span class='av_ajax_search_image' {$iconfont}>"; $output .= $image; $output .= '</span>'; } $output .= "<span class='av_ajax_search_content'>"; $output .= "<span class='av_ajax_search_title'>"; $output .= get_the_title($post->ID); $output .= '</span>'; if( $excerpt !== '' ) { $output .= "<span class='ajax_search_excerpt'>"; $output .= $excerpt; $output .= '</span>'; } $output .= '</span>'; $output .= '</a>'; } } $output .= "<a class='av_ajax_search_entry av_ajax_search_entry_view_all' href='{$search_messages['all_results_link']}'>{$search_messages['view_all_results']}</a>"; echo $output; die(); } }
please look for the line
'no_criteria_matched' => __( 'Sorry, no match', 'avia_framework' ),
&'another_search_term' => __( 'Please try again', 'avia_framework' ),
and adjust to suit.
I tested this in my child theme and as you can see from the screenshot it works.Best regards,
MikeNovember 27, 2020 at 3:50 pm #1263541Hi Mike,
I’m coming back to this very old thread just to tell you that your function works great.
Thanks for your help!
Best regardsNovember 28, 2020 at 12:57 pm #1263669Hi,
Glad we were able to help, we will close this now. Thank you for using Enfold.For your information, you can take a look at Enfold documentation here
For any other questions or issues, feel free to start new threads in the Enfold forum and we will gladly try to help you :)Best regards,
Mike -
AuthorPosts
- The topic ‘Modify text for search with no results’ is closed to new replies.