Php 限制摘录长度的自定义函数正在将文章标题作为摘录(WP)的一部分返回
我在functions.php中添加了以下内容:Php 限制摘录长度的自定义函数正在将文章标题作为摘录(WP)的一部分返回,php,wordpress,function,Php,Wordpress,Function,我在functions.php中添加了以下内容: function excerpt($limit) { global $id; $excerpt = explode(' ', get_the_excerpt(), $limit); if (count($excerpt)>=$limit) { array_pop($excerpt); $excerpt = implode(" ",$excerpt).'... <p class="readmore"><
function excerpt($limit) {
global $id;
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'... <p class="readmore"><a href="'. get_permalink($id) . '" title="' . the_title_attribute( array('echo' => 0, 'before' => 'Permalink to: ', 'after' => '')) . the_title() . '">Read More »</a></p>';
} else {
$excerpt = implode(" ",$excerpt).'... <p class="readmore"><a href="'. get_permalink($id) . '" title="' . the_title_attribute( array('echo' => 0, 'before' => 'Permalink to: ', 'after' => '')) . the_title() . '">Read More »</a></p>';
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
函数摘录($limit){
全球$id;
$EXECRPT=爆炸(“”,获取_EXECRPT(),$limit);
如果(计数($摘录)>=$limit){
数组_pop($摘录);
$EXECRPT=内爆(“,$EXECRPT)。”…;
}否则{
$EXECRPT=内爆(“,$EXECRPT)。”…;
}
$EXECRPT=预替换('\[^\]*\]`,''.$EXECRPT);
返回$摘录;
}
出于某种原因,它会将文章标题作为摘录的一部分返回(文章标题排在第一位,然后是摘录,标题和摘录之间没有空格-,此处示例:)。有人能告诉我我做错了什么吗?在functions.php文件中添加以下代码,以获取摘录和内容
$excerpt = implode(" ",$excerpt)
. '... <p class="readmore"><a href="'
. get_permalink($id)
. '" title="'
. the_title_attribute(array(
'echo' => 0,
'before' => 'Permalink to: ',
'after' => ''))
### change is in this line. (the_title() removed)
. '">Read More »</a></p>'
;
<?php
// Custom Excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
// Content Limit
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
?>
现在,不要在循环中使用_content()或_摘录,而是使用摘录($limit)或内容($limit)
如果您想将摘录限制在300字以内,代码如下所示:
<?php echo excerpt(50); ?>
<?php echo content(300); ?>
我找到了另一种按角色显示有限摘录的方法。下面是functions.php文件代码
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_the_permalink().'">Read More</a>';
return $excerpt;
}
函数get_摘录(){
$EXECRPT=获取内容();
$EXECRPT=preg\U replace(“(\[.*?\])”,“$EXECRPT);
$EXECRPT=条带_短代码($EXECRPT);
$EXECRPT=带标签($EXECRPT);
$extract=substr($extract,0100);
$EXECRPT=substr($EXECRPT,0,STRIPOS($EXECRPT,“”));
$EXECRPT=修剪(预替换('/\s+/','$EXECRPT));
$EXECRPT=$EXECRPT.“…”;
返回$摘录;
}
在此之后,您需要添加要逐个字符显示自定义字符的位置
<?php echo get_excerpt(); ?>
资料来源:
<?php echo get_excerpt(); ?>