Custom excerpt from WYSIWYG advanced custom field leads to HTML markup issue
-
I’ve a custom excerpt function to get an excerpt from an advanced custom fields WYSIWYG field. The function inside functions.php is:
function custom_field_excerpt($title) { global $post; $text = get_field($title); if ( '' != $text ) { $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $excerpt_length = 35; // 20 words $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('the_excerpt', $text); }
the function is called in the html with:
<p class="bran-hn news-excerpt"><?php echo custom_field_excerpt('news_post'); ?></p>
the resulting html looks like:
<p class="bran-hn news-excerpt"></p> <p>Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p> <p></p>
but i would have expected as well as wanted:
<p class="bran-hn news-excerpt">Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>
After further research I guess the cause for the issue are that html tags are added by the WYSIWYG field. I tried to add:
$text = strip_tags( $text );
to the function from the beginning but no luck. but is there a way to get to that single line from above?
<p class="bran-hn news-excerpt">Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>
where only the wrapping p elements are stripped that e.g. anchor tags remain inside. Best regards Ralf
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Custom excerpt from WYSIWYG advanced custom field leads to HTML markup issue’ is closed to new replies.