该摘录删除了所有html标记,这一特性让许多人感到沮丧。我已经编写了一个自定义摘录,可以阻止该摘录触发任何html标记。它还会在设定的字数后打断句子后面的摘录。如果您需要在精确的单词处剪切摘录,只需修改我的代码即可。
您首先需要删除原始摘录,然后注册新摘录。为此,请在函数中添加以下代码。php
remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'pietergoosen_custom_wp_trim_excerpt\');
现在是自定义摘录。将此添加到上面的代码下面
function pietergoosen_custom_wp_trim_excerpt($pietergoosen_excerpt) {
global $post;
$raw_excerpt = $pietergoosen_excerpt;
if ( \'\' == $pietergoosen_excerpt ) {
$pietergoosen_excerpt = get_the_content(\'\');
$pietergoosen_excerpt = strip_shortcodes( $pietergoosen_excerpt );
$pietergoosen_excerpt = apply_filters(\'the_content\', $pietergoosen_excerpt);
$pietergoosen_excerpt = str_replace(\']]>\', \']]>\', $pietergoosen_excerpt);
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters(\'excerpt_length\', $excerpt_word_count);
$tokens = array();
$excerptOutput = \'\';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all(\'/(<[^>]+>|[^<>\\s]+)\\s*/u\', $pietergoosen_excerpt, $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
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what\'s left of the token
$excerptOutput .= $token;
}
$pietergoosen_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \' » \' . sprintf(__( \'Read more about: %s »\', \'pietergoosen\' ), get_the_title()) . \'</a>\';
$excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);
$pos = strrpos($pietergoosen_excerpt, \'</\');
if ($pos !== false)
// Inside last HTML tag
$pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
else
// After the content
$pietergoosen_excerpt .= $excerpt_end;
return $pietergoosen_excerpt;
}
return apply_filters(\'pietergoosen_custom_wp_trim_excerpt\', $pietergoosen_excerpt, $raw_excerpt);
}
如果您只需要特定的标记,可以使用下面的代码。只需添加下面的代码,然后删除需要剥离的标记。
function pietergoosen_get_allowedtags() {
// Add custom tags to this string that will be used as allowed tags
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>\';
}
只需添加此行
$pietergoosen_excerpt = strip_tags($pietergoosen_excerpt, pietergoosen_get_allowedtags());
此线以下
$pietergoosen_excerpt = str_replace(\']]>\', \']]>\', $pietergoosen_excerpt);
希望这对你有帮助