【问题标题】:Wordpress remove read more from excerptWordpress 从摘录中删除阅读更多内容
【发布时间】:2017-02-13 10:46:12
【问题描述】:

我遇到了 Remove_filter() 的问题。删除过滤器有效但不是 100%,它不会从 Read More 中删除前 3 个点,它不会替换父摘录。

更清楚我想要做的是删除 Read More 并将其更改为 [...]

我希望通过子主题完成此操作

这里是截图示例BeforeAfter

父主题functions.php代码

    function new_excerpt_more($more) {
    global $post;
    return '… <a href="'. get_permalink($post->ID) . '">' . 'Read More!   &raquo;'     . '</a>';
   }
    add_filter('excerpt_more', 'new_excerpt_more');

儿童主题functions.php代码。

       function child_theme_setup() {
       // override parent theme's 'more' text for excerpts
       remove_filter( 'excerpt_more', 'new_excerpt_more' ); 

      }
     add_action( 'after_setup_theme', 'child_theme_setup' );
    // Replaces the excerpt "Read More" text by a link
     function new_excerpt($more) {
     global $post;
     return '<a class="moretag" href="'. get_permalink($post->ID) . '">[...]                      </a>';
      }
      add_filter('excerpt', 'new_excerpt');

【问题讨论】:

  • 明确说明你的问题..
  • @HappyCoding,我编辑了它

标签: php wordpress


【解决方案1】:

您只需要稍微更新一下您的代码。

// Changing excerpt more
function new_excerpt_more($more) {
  global $post;
  remove_filter('excerpt_more', 'new_excerpt_more'); 
  return ' <a class="read_more" href="'. get_permalink($post->ID) . '">' . ' do whatever you want to update' . '</a>';
}
add_filter('excerpt_more','new_excerpt_more',11);

钩子的默认优先级是 10。父主题的钩子可能会被添加到第二个,这意味着您的钩子和父钩子具有相同的优先级,但父主题的优先级排在最后,因此它不反映更改。

【讨论】:

  • 还是一样,三个点还是一样。我写在那里的内容不会改变。
  • 更新钩子优先级为11..然后查看结果
【解决方案2】:

如果你不想写一个新函数,只希望它在特定区域工作,你也可以试试 wp_trim_words() 函数结合 get_the_content()

例子:

<?php echo wp_trim_words(get_the_content(), 60) ?>

上面的操作是将 get_the_content() 返回的任何单词缩减为您想要的单词数(在本例中为 60)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2013-11-30
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2017-02-26
    相关资源
    最近更新 更多