Show read more link next to last word

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(']]>', ']]&gt;', $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() ) . '">' . '&hellip;' . __( 'Read more about this article <span class="meta-nav">&rarr;</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.

Related posts

2 comments

  1. The only thing that makes sense to me is that $text immediately after this line:

    $text = force_balance_tags($out);
    

    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.

    $text = trim($text) . $excerpt_end;
    
  2. Thanks for your answer @s_ha_dum. I eventually got it working. I changed $text = $text . $excerpt_end; to

    $pos = strrpos($text, '</');
    
        // Add 'Read more' text inside last HTML tag
        $text = substr_replace($text, $excerpt_end, $pos, 0);
    

Comments are closed.