有没有办法在某些帖子上覆盖“将评论分解为页面”和“关闭旧帖子的评论”,而不是在其他帖子上?

时间:2017-04-21 作者:ioutshine

在“设置”>“讨论”中,有一个“将评论拆分为页面”的选项,可以为评论添加分页并将其拆分为单独的页面。

我想在整个网站上关闭此功能。。。但是,在一篇特定的文章中,我希望打开它,以便该文章(仅限该文章)对评论进行分页。

这能做到吗?

半相关跟进:是否可以使用“自动关闭早于的帖子的评论”选项来完成同样的事情?在这种情况下,除了149、150和151号帖子外,所有帖子的评论都会在X天后关闭?

提前感谢您的帮助。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

这个option_{$option_name} 过滤器可用于动态修改选项的值。代替{$option_name} 要修改的选项的名称。

覆盖注释分页选项

分页注释的选项名称为page_comments, 因此,我们将创建一个名为option_page_comments. 在下面的示例中,我们检查是否正在查看ID数组中的一篇文章以强制分页,如果是,我们将强制对注释分页。否则,将使用“Dashboard”>“Settings”>“Discussion”屏幕中的值。

// Forces comment pagination for certain posts regardless
// of settings within the Settings > Discussion page.
add_filter( \'option_page_comments\', \'wpse_modify_page_comments\' );
function wpse_modify_page_comments( $page_comments ) {
    if ( is_admin() ) {
        return $page_comments;
    }

    // Array of post IDs where comment pagination is forced on.
    $force_comment_pagination = [ 
        149,
        150,
        151,
    ];

    if ( in_array( get_the_ID(), $force_comment_pagination ) ) {
        $page_comments = true;
    }

    return $page_comments;
}
覆盖旧帖子的关闭评论选项来回答您的后续问题——是的,我们可以强制为某些旧帖子启用评论,即使讨论屏幕上的设置配置为关闭旧帖子的评论。

// Forces comments for old posts to be *allowed* regardless
// of settings within the Settings > Discussion page.
add_filter( \'option_close_comments_for_old_posts\', \'wpse_modify_close_comments_for_old_posts\' );
function wpse_modify_close_comments_for_old_posts( $close_comments_for_old_posts ) {
    // Don\'t do anything for the admin area. Return the originally set value of the option.
    if ( is_admin() ) {
        return $close_comments_for_old_posts;
    }

    // This array contains the posts IDs where we want to 
    // override the settings for closing comments for old posts.
    // (Comments will be forced open for these posts.)
    $close_comments_for_old_posts_overrides = [ 
        149,
        150,
        151,
    ];

    // Handle case when a comment is being made.
    if ( isset( $_POST[\'comment\'] ) && isset( $_POST[\'comment_post_ID\'] ) ) {
        if ( in_array( $_POST[\'comment_post_ID\'], $close_comments_for_old_posts_overrides ) ) {
            // Comments should be open for this post.
            return false;
        }               
    }

    // Handle case when post is displayed.
    global $wp_query;
    if ( ! is_array( $wp_query->posts ) ) {
        // There are no posts to display. Don\'t change the option.
        return $close_comments_for_old_posts;
    }
    foreach ( $wp_query->posts as $post ) {
        if ( in_array( $post->ID, $close_comments_for_old_posts_overrides ) ) {
            // Comments should be open for this post.
            return false;
        }
    }

    // If we get here, return the original value of the option without altering it.
    return $close_comments_for_old_posts;
}

相关推荐

Geoip shortcodes in comments

我想知道如何从geoip插件添加国家/地区短代码(https://pl.wordpress.org/plugins/geoip-detect/) 输入注释字段。[geoip\\u detect2 property=“country”]据我所知,注释字段必须是所见即所得字段(默认情况下不是文本)。还有其他方法吗?通过自定义php函数或其他方式?你好,Michal