子比主题关闭文章评论功能保留页面评论功能

文章最后更新时间:2025-09-20 20:07:52

wordpress

 

意思就是不让用户评论了,评论关闭了,但是之前评论的内容还显示

代码部署

放在func.php文件即可

// 关闭文章评论但保留页面评论(修复版)
function zib_custom_comment_status($open, $post_id) {
    // 确保$post_id有效
    if (!$post_id) return $open;
    
    $post = get_post($post_id);
    // 检查$post是否有效
    if (!$post) return $open;
    
    // 关闭文章评论,开启页面评论
    if ($post->post_type == 'post') {
        return false;
    } elseif ($post->post_type == 'page') {
        return true;
    }
    
    return $open;
}
add_filter('comments_open', 'zib_custom_comment_status', 20, 2);

// 新文章默认关闭评论
function zib_default_post_comment_status($data, $postarr) {
    if ($data['post_type'] == 'post' && empty($postarr['comment_status'])) {
        $data['comment_status'] = 'closed';
    }
    return $data;
}
add_filter('wp_insert_post_data', 'zib_default_post_comment_status', 10, 2);

// 新页面默认开启评论
function zib_default_page_comment_status($data, $postarr) {
    if ($data['post_type'] == 'page' && empty($postarr['comment_status'])) {
        $data['comment_status'] = 'open';
    }
    return $data;
}
add_filter('wp_insert_post_data', 'zib_default_page_comment_status', 10, 2);

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0
评论 抢沙发

请登录后发表评论

    暂无评论内容