Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #156569
    ebetech
    Participant

    I have created some custom shortcodes that displays an event’s date or location. The shortcodes work great with no errors. Problem is when you use the site’s search tool, the results do not display the shortcode’s content. All results are Pages, not Posts.

    === Example 1 ===

    (Shortcode for Event Date = SATURDAY, AUGUST 4th) 
    
    Page Displays:
    SATURDAY, AUGUST 4th
    3:00 PM – 6:00 PM
    Home Arts
    
    Results Excerpt Displays:
    3:00 PM – 6:00 PM
    Home Arts

    === END Example 1 ===

    === Example 2 ===

    (Shortcode for Event Location = GENERAL PARKING) 
    
    Page Displays:
    GENERAL PARKING
    - Express Train
    
    Results Excerpt Displays:
    - Express Train

    === END Example 2 ===

    I tried adding the following — per link https://catchthemes.com/support-forum/topic/text-within-shortcodes-removed-from-front-page-excerpts/

    add_filter( 'the_excerpt', 'shortcode_unautop');
    add_filter( 'the_excerpt', 'do_shortcode');

    But did not work.

    What can I do to resolve this issue?

    Thanks!

    #156593
    Skandha
    Participant

    @ebetech: Can you please post in your site URL so that I can look into the issue.

    Kind Regards,
    Skandha

    #156812
    ebetech
    Participant

    Never mind. After some research, I found the solution. It appears to be a WordPress core design. To display the shortcode content in results, add the following to your functions.php:

    //Replace wp_trim_excerpt with a commented out strip_shortcodes()
    function improved_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    
    		//$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    		if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('improved_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');

    Thanks for the quick reply. Awesome theme. Awesome support.

    #157095
    Skandha
    Participant

    @ebetech: Glad you were able to resolve the issue. Have a good day! 🙂

    Kind Regards,
    Skandha

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Search Results not displaying Shortcode content’ is closed to new replies.