php - 如何检测WordPress中是否存在手动摘录?

标签 php wordpress

我需要显示不同长度的摘录,所以我使用

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/

相关文章:

javascript - $.get json数组复制

php - iOS 存折后栏中的特殊字符

php - Paypal IPN 不断重试,但通知 url 甚至没有得到回调

WordPress : get iso language

html - 不为其他人加载的 Css 文件

php - 如何确保php对wp专用服务器有写权限

php - 从页面 facebook api graph 获取信息

php - Codeigniter:加载 session 库时出现错误

css - 如何实现4栏内容,所有内容都聚在一起?

php - 使用查询字符串和 anchor 标签形成正确的 URL