Показать ссылку "Подробнее" рядом с последним словом


У меня есть эта функция извлечения

     <?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'); 
?>

Все работает отлично, как задумано, за исключением того, что Read more about this article... появляется в отдельном абзаце после отрывка, а не рядом с последним словом, как в отрывке по умолчанию. В Google я вижу, что wordpress добавляет тег <p> вокруг Read more about this article Как я могу это исправить?

 1
Author: Pieter Goosen, 2014-02-18

2 answers

Единственное, что имеет для меня смысл, это то, что $text сразу после этой строки:

$text = force_balance_tags($out);

Имеет что-то, что wpautop переводится как разрыв абзаца - что-то вроде двойной новой строки.

Непроверенный, но я бы подумал, что trim это все прояснило бы.

$text = trim($text) . $excerpt_end;
 3
Author: s_ha_dum, 2014-02-18 21:57:35

Спасибо за ваш ответ @s_ha_dum. В конце концов я заставил его работать. Я изменил $text = $text . $excerpt_end; на

$pos = strrpos($text, '</');

    // Add 'Read more' text inside last HTML tag
    $text = substr_replace($text, $excerpt_end, $pos, 0);
 1
Author: Pieter Goosen, 2014-02-19 18:05:16