在最后一个单词旁边显示阅读更多链接

时间:2014-02-18 作者:Pieter Goosen

我有这个摘录功能

     <?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 我怎样才能解决这个问题。

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

感谢您的回答@s\\u ha\\u dum。我终于让它工作了。我改变了$text = $text . $excerpt_end;

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

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

SO网友:s_ha_dum

对我来说唯一有意义的是$text 紧跟在这一行之后:

$text = force_balance_tags($out);
有些东西wpautop 翻译成一个段落分隔符——有点像双换行符。

未经测试,但我认为trim 会把它清理干净的。

$text = trim($text) . $excerpt_end;

结束

相关推荐

Bold letters inside excerpt

我目前正在尝试将摘录中的一些单词用“h1”标记加粗,但它似乎不起作用。当我在网上查看时,我所听到的只是建议使用插件。有没有一种方法可以在没有插件的情况下实现这一点。我的CSS如下所示。.side-bar h1 { font-weight: bold!important; }