【问题标题】:Trim user entered excerpt in content.php wordpress file修剪用户在 content.php wordpress 文件中输入的摘录
【发布时间】:2018-09-13 04:00:48
【问题描述】:

我有一个自定义函数来修剪摘录:

function custom_excerpt_length( $length ) {
return 10; }

以及应用它的过滤器:

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

然后我的 content.php 中的摘录如下:

<?php the_excerpt(); ?>

如果未找到自定义用户输入的摘录,该功能将起作用。所以基本上是从博文内容中抓取前 10 个单词。但是当我自定义输入摘录时它不会修剪。有什么想法可以在博客文章页面中显示用户输入的摘录版本?

【问题讨论】:

  • 你说“当我自定义输入摘录时”是什么意思?您自定义输入摘录的过程是什么?
  • [链接] (imgur.com/1UwZhdx)
  • 添加新帖子 > 添加内容 > 添加摘录。所以基本上如果我在创建新帖子时将摘录字段留空,custom_excerpt_lenght 函数就会起作用。

标签: php wordpress


【解决方案1】:

我终于得到了我需要的东西。所以我只想将修剪后的摘录减少到较低的字符数。我刚刚添加到我的功能页面:

function custom_excerpt_more( $more ) {
    $more = '&hellip;';
    return $more; 
}
add_filter( 'excerpt_more', 'custom_excerpt_more' ); //displays ... after the excerpt

function custom_excerpt_length( $length ) {
return 30; //number of words to display in the excerpt
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); //999 si it filters at the end

然后在我的 content.php 页面中:

<p><?php echo substr(get_the_excerpt(),0,80);?></p>

这样我的博客文章列表页面中的字符数不超过 80 个,而我的个人博客文章页面 (content-single.php) 中的单词数不超过 30 个。

【讨论】:

  • 只是添加评论说这真的很有帮助,谢谢。不知道为什么 WP 不包含用于修剪自定义摘录的内置函数
  • substr 为我做了这件事,当涉及到用户手动输入且不是由 WP 自动生成的摘录时。对于自动生成的摘录,过滤器 excerpt_length 效果很好。
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多