我需要显示不同长度的摘录,所以我使用
function custom_excerpt($length) {
get_the_content();
... clean up and trim
return $excerpt;
}
但是,我想检测是否输入了手动摘录以便使用它而不是自定义摘录。有没有办法做到这一点?
我尝试使用
$wp_excerpt = get_the_excerpt();
但这会返回手动摘录,如果手动摘录为空,它会自动生成 55 个字符的摘录,这无济于事,因为它将始终为“真”(无法检查是否为空)。
这样做的原因是因为我在一个页面上有多个摘录(不同长度),如果需要的长度比 WordPress 摘录(55)长,我想展示我的摘录,除非 写了一个手册摘录,在这种情况下,我想证明这一点。
如果我能简单地就完美了
if ( manual_excerpt() == true ) {
}
最佳答案
你只需要替换wordpress默认的excerp_length函数,按照上面的代码,然后你就可以调用这个自定义函数并设置长度:
<?php
// custom excerpt length
function custom_excerpt_length( $length = 20 ) {
return $length;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
?>
答案更新二在函数内部使用:
<?php
function custom_excerpt( $length = 55 ) {
if( $post->post_excerpt ) {
$content = get_the_excerpt();
} else {
$content = get_the_content();
$content = wp_trim_words( $content , $length );
}
return $excerpt;
}
?>
关于php - 如何检测WordPress中是否存在手动摘录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19935351/