子比主题摘要函数优化

子比主题摘要函数优化

背景

wordpress子比主题+分类密码插件(https://barn2.com/wordpress-plugins/password-protected-categories/)

问题

现在付费阅读、加密的分类、公开的文章,摘要都不显示了

需求

优化附件代码,实现加密分类、付费内容(登录了已购买的账号除外)未授权会员、非加密分类的加密文章,这4类的摘要不显示。公开的摘要显示

解决方案

位置:/www/wwwroot/xxx/wp-content/themes/zb_yt/inc/functions/zib-theme.php 搜索:function zib_get_excerpt  用下面代码替换掉function zib_get_excerpt函数内容即可

function zib_get_excerpt($limit = 90, $after = '...', $post = null) {
    $protected_excerpt = apply_filters(
        'zib_protected_excerpt_text',
        '内容无法查看,请验证权限(或密码)后查看内容',
        $post
    );
 
    // 初始化并获取文章对象
    if (!is_object($post)) {
        $post = get_post($post);
    }
 
    if (!$post) {
        return '';
    }
 
    // ======== 增强分类密码检查 ========
    $protected_by_category = false;
    $taxonomies = get_object_taxonomies($post->post_type);
    foreach ($taxonomies as $taxonomy) {
        $terms = get_the_terms($post->ID, $taxonomy);
        if ($terms && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $term_password = get_term_meta($term->term_id, 'category_password', true);
                if (!empty($term_password)) {
                    $protected_by_category = true;
 
                    // 钩子:检查用户是否有分类访问权限
                    $has_access = apply_filters('zib_has_term_password_access', false, $post->ID, $term->term_id);
                    if ($has_access) {
                        $protected_by_category = false;
                    }
                    break 2;
                }
            }
        }
    }
 
    // ======== 付费内容检查 ========
    $is_paid_content = false;
    $pay_type = get_post_meta($post->ID, 'pay_type', true);
    $logged_in_pay = get_post_meta($post->ID, 'logged_in_pay', true);
 
    if (!empty($pay_type) && $pay_type !== 'no') {
        $is_paid_content = true;
 
        // 判断是否为已购买用户(你可以根据自己的付费插件逻辑修改这部分)
        if (is_user_logged_in()) {
            // 示例:假设你使用子比主题自带的付费功能,已购买用户可查看 
            // 你可以根据实际情况扩展此判断
            $is_paid_content = !apply_filters('zib_user_has_paid_access', false, $post->ID);
        }
    }
 
    // ======== 文章本身加密检查 ========
    $is_post_protected = post_password_required($post);
 
    // ======== 会员权限检查 ========
    $is_non_public = (
        $is_post_protected ||
        $protected_by_category ||
        $is_paid_content ||
        (!empty($logged_in_pay) && !is_user_logged_in())
    );
 
    // 扩展钩子,便于其他插件主题介入权限判断
    $is_non_public = apply_filters('zib_excerpt_is_non_public', $is_non_public, $post);
 
    // 返回加密提示(核心修改点)
    if ($is_non_public) {
        return '<p class="excerpt-protected">' . $protected_excerpt . '</p>';
    }
 
    // ======== 原始摘要处理逻辑(保持原样) ========
    if (has_excerpt($post->ID)) {
        $excerpt = $post->post_excerpt;
    } else {
        $content = $post->post_content;
        $excerpt = wp_strip_all_tags(strip_shortcodes($content));
    }
 
    return zib_str_cut($excerpt, 0, $limit, $after);
}

 

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

请登录后发表评论

    暂无评论内容