0

我想在我的 WordPress 摘录中加入换行符。

为此,我看到我可以更改此功能:

function wp_strip_all_tags($string, $remove_breaks = false) {
  $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  $string = strip_tags($string);

  if ( $remove_breaks )
    $string = preg_replace('/[\r\n\t ]+/', ' ', $string);

  return trim( $string );
}

至:

function wp_strip_all_tags_breaks($string, $remove_breaks = false) {
  $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  $string = strip_tags($string, '<p>');

  if ( $remove_breaks )
    $string = preg_replace('/[\r\n\t ]+/', ' ', $string);

  return trim( $string );
}

修改我的主题以切换功能并提供此功能的最佳方法是什么?

4

1 回答 1

1

Overriding/overloading any of the WordPress core functions has to be done in the functions.php of your current theme.

First you have to define the new function in the functions.php (the name should be different from the original wpcore function name) and then you have remove the old function and add the new function to the respective hook/filter.

In case of the_excerpt() it should be done like this:

function new_function() {
    //code here
}

remove_filter('get_the_excerpt', 'old_function');
add_filter('get_the_excerpt', 'new_function');

Hope that makes sense.

EDIT: Here is a good tutorial on how to edit the_excerpt() formatting.

于 2013-06-30T05:12:02.070 回答