我注意到的某件事是,我不认为我做错了,但这不能按预期工作。
通常,使用get_the_excerpt()
时,您可以在循环外传递帖子ID。
但是,我假设如果我在循环内使用此功能,但是将ID提供给我想要的任何页面,它将返回该帖子ID的摘录。
但是,返回的只是当前帖子(就像我在循环中一样)。但这肯定不是预期的行为吗?
如果我在循环中
答案 0 :(得分:0)
好吧,研究wp_trim_excerpt函数,该函数过滤get_the_excerpt
function wp_trim_excerpt( $text = '' ) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]>', $text);
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/**
* Filters the trimmed excerpt string.
*
* @since 2.8.0
*
* @param string $text The trimmed text.
* @param string $raw_excerpt The text prior to trimming.
*/
return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}
此处的$ text等于$ post-> post_excerpt,如果为空,则使用get_the_content()
因此,答案是填写自定义摘录字段或执行类似操作
function custom_excerpt($text, $post = '') {
if ( '' == $text ) {
if ( !empty($post) ) {
$text = $post->post_content;
} else {
$text = get_the_content('');
}
$text = strip_shortcodes( $text );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]>', $text);
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
return $text;
}
return $text;
}
add_filter('get_the_excerpt', 'custom_excerpt', 10, 2);