I have this excerpt function
<?php
function pietergoosen_get_the_content_limit_custom_allowedtags() {
// Add custom tags to this string
return '<head>,<title>,<base>,<link>,<meta>,<style>,<script>,<noscript>,<body>,<section>,<nav>,
<article>,<aside>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<header>,<footer>,<address>,<main>,<p>,<hr>,
<pre>,<blockquote>,<ol>,<ul>,<li>,<dl>,<dt>,<dd>,<figure>,<figcaption>,<div>,<a>,<em>,<strong>,
<small>,<s>,<cite>,<q>,<dfn>,<abbr>,<data>,<time>,<code>,<var>,<samp>,<kbd>,<sub>,<sup>,<i>,<b>,
<u>,<mark>,<ruby>,<rt>,<rp>,<bdi>,<bdo>,<span>,<br>,<wbr>,<ins>,<del>,<img>,<iframe>,<embed>,
<object>,<param>,<video> ,<audio>,<source>,<track>,<canvas>,<map>,<area>,<svg>,<math>,<table>,
<caption>,<colgroup>,<col>,<tbody>,<thead>,<tfoot>,<tr>,<td>,<th>,<form>,<fieldset>,<legend>,<label>,
<input>,<button>,<select>,<datalist>,<optgroup>,<option>,<textarea>,<keygen>,<output>,<progress>,<meter>,
<details>,<summary>,<menuitem>,<menu>';
}
function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
//Add the allowed HTML tags separated by a comma.
$text = strip_tags($text, pietergoosen_get_the_content_limit_custom_allowedtags());
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '…' . __( 'Read more about this article <span class="meta-nav">→</span>', 'pietergoosen' ) . '</a>';
$tokens = array();
$out = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>s]+)s*/u', $text, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_word_count && preg_match('/[?.!]s*$/uS', $token)) {
// Limit reached, continue until ? . or ! occur at the end
$out .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$out .= $token;
}
$text = force_balance_tags($out);
$text = $text . $excerpt_end;
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt');
?>
Everything works great as intented, except that the Read more about this article...
appear in a separate paragrahp after the excerpt, and not next to the last word as in the default excerpt. In google I can see that wordpress add a <p>
tag around the Read more about this article
How can I fix this.
The only thing that makes sense to me is that
$text
immediately after this line:Has something that
wpautop
translates as a paragraph break– something like a double newline.Untested, but I would think that
trim
would clear it up.Thanks for your answer @s_ha_dum. I eventually got it working. I changed
$text = $text . $excerpt_end;
to