坚持学习
我们一定有收获

WordPress隐藏内容回复可见纯代码教程

DeepSeek探索全新的WordPress-AI插件(支持定制功能)点击立即了解

WordPress隐藏内容回复可见纯代码教程

// 添加短代码
function hidden_content_shortcode( $atts, $content = null ) {
    global $post;
    $user_can_view = false;

    if ( is_user_logged_in() ) {
        $comments = get_comments( array(
            'post_id' => $post->ID,
            'user_id' => get_current_user_id()
        ) );
        if ( ! empty( $comments ) ) {
            $user_can_view = true;
        }
    }

    if ( $user_can_view ) {
        return $content;
    } else {
        return '<p>此内容需回复文章后才能查看。</p>';
    }
}
add_shortcode( 'hidden_content', 'hidden_content_shortcode' );    

在文章编辑中,使用 [ hidden_content ] 短代码包裹需要隐藏的内容,例如:

这是公开内容。
[ hidden_content ]
这是隐藏内容,回复后可见。
[ /hidden_content ]
内容仅供参考:栈学 » WordPress隐藏内容回复可见纯代码教程

评论 抢沙发

登录

找回密码

注册