What is your theme doing to print out the excerpt in the search results template? Looks like the theme might be escaping the HTML tags, which is often a good idea, but not in this case, when the HTML codes should be displayed on the page.
Thread Starter
rely28
(@rely28)
I’m not sure, here is what I have in the excerpt.php
<?php
if ( post_password_required() ) {
echo get_the_password_form();
} else {
$excerpt_length = isset( $params['excerpt_length'] ) ? $params['excerpt_length'] : '';
$excerpt = backpacktraveler_mikado_excerpt( $excerpt_length );
$link_page_exists = apply_filters( 'backpacktraveler_mikado_filter_single_links_exists', '' );
if ( ! empty( $excerpt ) && empty( $link_page_exists ) ) { ?>
<div class="mkdf-post-excerpt-holder">
<p itemprop="description" class="mkdf-post-excerpt">
<?php echo wp_kses_post( $excerpt ); ?>
</p>
</div>
<?php }
} ?>
Thread Starter
rely28
(@rely28)
In the blog-function.php I have this :
if ( ! function_exists( 'backpacktraveler_mikado_excerpt' ) ) {
/**
* Function that cuts post excerpt to the number of word based on previosly set global
* variable $word_count, which is defined in mkdf_set_blog_word_count function.
*
* @param $length - default excerpt length
*
* @return string - formatted excerpt
*
* It current post has read more tag set it will return content of the post, else it will return post excerpt
*
*/
function backpacktraveler_mikado_excerpt( $length ) {
global $post;
//does current post has read more tag set?
if ( backpacktraveler_mikado_post_has_read_more() ) {
global $more;
//override global $more variable so this can be used in blog templates
$more = 0;
return get_the_content( true );
}
$number_of_chars = backpacktraveler_mikado_get_meta_field_intersect( 'number_of_chars', backpacktraveler_mikado_get_page_id() );
$word_count = $length !== '' ? $length : $number_of_chars;
//is word count set to something different that 0?
if ( $word_count > 0 ) {
//if post excerpt field is filled take that as post excerpt, else that content of the post
$post_excerpt = $post->post_excerpt !== '' ? $post->post_excerpt : strip_tags( strip_shortcodes( $post->post_content ) );
//remove leading dots if those exists
$clean_excerpt = strlen( $post_excerpt ) && strpos( $post_excerpt, '...' ) ? strstr( $post_excerpt, '...', true ) : $post_excerpt;
//if clean excerpt has text left
if ( $clean_excerpt !== '' ) {
//explode current excerpt to words
$excerpt_word_array = explode( ' ', $clean_excerpt );
//cut down that array based on the number of the words option
$excerpt_word_array = array_slice( $excerpt_word_array, 0, $word_count );
//and finally implode words together
$excerpt = implode( ' ', $excerpt_word_array );
//is excerpt different than empty string?
if ( $excerpt !== '' ) {
return rtrim( wp_kses_post( $excerpt ) );
}
}
return '';
} else {
return '';
}
}
}
You can try replacing
<?php echo wp_kses_post( $excerpt ); ?>
with
<?php the_excerpt(); ?>
Thread Starter
rely28
(@rely28)
But If I do so, I’m afraid it’s gonna change all of the actual exceprt, not just the ones from the search list, isn’t it ?
Thread Starter
rely28
(@rely28)
The strip tags is actually in the blog-function.php
$post_excerpt = $post->post_excerpt !== '' ? $post->post_excerpt : strip_tags( strip_shortcodes( $post->post_content ) );
I believe this is the culprit, but I’m afraid to messed up the functionality if removing or changing improperly…
Thread Starter
rely28
(@rely28)
By the way, I did try your solution, in case, with no luck…
No, strip_tags() isn’t the problem, because the tags are not stripped, they’re escaped. Also, Relevanssi sets $post->post_excerpt, so that’s what the code uses. If the tags were stripped, that would look very nice and clean. This looks like the work of esc_html(), but there doesn’t seem to be esc_html() anywhere there.
In any case, the best solution would be to simply use the_excerpt() for the search results in the search results template, because that would get you just the Relevanssi-generated excerpts, without any problems whatsoever.
Thread Starter
rely28
(@rely28)
Ok, That’s a useful information since I remembered seing it somewhere, and here is the file I have, loop.php
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="mkdf-post-content">
<?php if ( has_post_thumbnail() ) { ?>
<div class="mkdf-post-image">
<a itemprop="url" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
</div>
<?php } ?>
<div class="mkdf-post-title-area <?php if ( ! has_post_thumbnail() ) { echo esc_attr( 'mkdf-no-thumbnail' ); } ?>">
<div class="mkdf-post-title-area-inner">
<h5 itemprop="name" class="mkdf-post-title entry-title">
<a itemprop="url" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h5>
<?php
$mkdf_my_excerpt = get_the_excerpt();
if ( ! empty( $mkdf_my_excerpt ) ) { ?>
<p itemprop="description" class="mkdf-post-excerpt"><?php echo wp_trim_words( esc_html( $mkdf_my_excerpt ), 30 ); ?></p>
<?php } ?>
</div>
</div>
</div>
</article>
<?php endwhile; ?>
<?php else: ?>
<p class="mkdf-blog-no-posts"><?php esc_html_e( 'No posts were found.', 'backpacktraveler' ); ?></p>
<?php endif; ?>
The only problem remaining is that, if I remove that esc_html, I indeed don’t see html anymore but the highlight is not working then…
wp_trim_words() also removes HTML tags. Replacing
echo wp_trim_words( esc_html( $mkdf_my_excerpt ), 30 );
with
echo $mkdf_my_excerpt;
should work just fine.
Thread Starter
rely28
(@rely28)
Awesome ! That made the trick ! Thanks a lot, gonna put 5 stars to your plugin straight away !