如下图所示,游客以及非VIP会员不可访问,游客访问跳转登录,非vip跳转会员开通。
效果见本站
将下载的附件中代码复制并放在主题根目录 functions.php 文件最底部。
找到\inc\codestar-framework\options\options.theme.php文件
在2785行插入代码
array(
‘id’ => ‘_vipcate’,
‘type’ => ‘select’,
‘title’ => ‘仅会员可见分类’,
‘placeholder’ => ‘选择分类’,
‘chosen’ => true,
‘multiple’ => true,
‘options’ => ‘categories’,
),
第二步:在主题functions.php文件尾部插入以下代码
function yite_entry_header( $options = array(),$id) {
$CaoUser = new CaoUser($id);
$vipcate == false;
$options = array_merge( array( ‘outside_loop’ => false, ‘container’ => ‘header’, ‘tag’ => ‘h2’, ‘link’ => true, ‘white’ => false, ‘author’ => false, ‘category’ => false, ‘date’ => false, ‘comment’ => false, ‘like’ => false ), $options );
$queried_object = get_queried_object();
$post_id = $options[‘outside_loop’] ? $queried_object->ID : get_the_ID();
$categories = get_the_category( $post_id ); ?>
<?php echo ‘<‘ . $options[‘container’] . ‘ class=”entry-header’ . esc_attr( $options[‘white’] ? ‘ white’ : ” ) . ‘”>’; ?>
<?php if ( $options[‘author’] || $options[‘category’] || $options[‘date’] || $options[‘comment’] || $options[‘like’] ) : ?>
<div class=”entry-meta”>
<?php if ( $options[‘author’] ) :
$author_id = get_post_field( ‘post_author’, $post_id ); ?>
<span class=”meta-author”>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_author_posts_url( get_the_author_meta( ‘ID’, $author_id ) ) ); ?>”>
<?php
echo get_avatar( get_the_author_meta( ’email’, $author_id ), ’40’, null, get_the_author_meta( ‘display_name’, $author_id ) );
echo get_the_author_meta( ‘display_name’, $author_id );
?>
</a>
</span>
<?php endif;
if ( $categories && $options[‘category’] ) : ?>
<span class=”meta-category”>
<?php foreach ( $categories as $key=>$category ) :
if ($key == 3) {break;}
?>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_category_link( $category->term_id ) ); ?>” rel=”category”>
<i class=”dot”></i><?php echo esc_html( $category->name ); ?>
</a>
<!–判断是否VIP分类–>
<?php
if($vipcate == false){
$vipcate = in_array($category->term_id,_cao(‘_vipcate’));
if($vipcate == true){
$cateid = $category->term_id;
}
}
?>
<?php endforeach; ?>
<?php
if($vipcate == true){
if ($CaoUser->vip_status()) {
$vipcate = false;
}
}
?>
</span>
<?php endif;
if ( $options[‘date’] ) : ?>
<span class=”meta-date”>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_the_permalink( $post_id ) ); ?>”>
<time datetime=”<?php echo esc_attr( get_the_date( ‘c’, $post_id ) ); ?>”>
<?php
// echo esc_html( get_the_date( null, $post_id ) );
echo _timeago( get_gmt_from_date(get_the_time(‘Y-m-d G:i:s’)) );
?>
</time>
</a>
</span>
<?php endif;
if ( $options[‘comment’] && ! post_password_required( $post_id ) && ( comments_open( $post_id ) || get_comments_number( $post_id ) ) ) : ?>
<span class=”meta-comment”>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_the_permalink( $post_id ) . ‘#comments’ ); ?>”>
<?php printf( _n( ‘%s 评论’, ‘%s 评论’, esc_html( get_comments_number( $post_id ) ), ‘cao’ ), esc_html( number_format_i18n( get_comments_number( $post_id ) ) ) ); ?>
</a>
</span>
<?php endif;?>
</div>
<?php endif; ?>
<?php
if($vipcate == true){
echo ‘<‘ . $options[‘tag’] . ‘ class=”entry-title”><a’. _target_blank() .’ href=”‘ . get_category_link($cateid) . ‘” title=”VIP分类资源” rel=”bookmark” class=”grida”>VIP分类资源,点击开通VIP后可访问</a></’ . $options[‘tag’] . ‘>’;
}else{
if ( $options[‘link’] ) {
echo ‘<‘ . $options[‘tag’] . ‘ class=”entry-title”><a’. _target_blank() .’ href=”‘ . esc_url( get_permalink( $post_id ) ) . ‘” title=”‘.get_the_title( $post_id ).'” rel=”bookmark”>’ . get_the_title( $post_id ) . ‘</a></’ . $options[‘tag’] . ‘>’;
} else {
echo ‘<‘ . $options[‘tag’] . ‘ class=”entry-title”>’ . get_the_title( $post_id ) . ‘</’ . $options[‘tag’] . ‘>’;
}
}
?>
<?php echo ‘</’ . $options[‘container’] . ‘>’;
}
// 获取图片高度 //NEW
function yite_entry_media($postid,$id) {
$CaoUser = new CaoUser($id);
$vipcate == false;
$categories = get_the_category( $postid );
for($i = 0;$i<count($categories);$i++){
if($vipcate == false){
$vipcate = in_array($categories[$i]->term_id,_cao(‘_vipcate’));
if($vipcate == true){
$cateid = $categories[$i]->term_id;
}
}
}
if($vipcate == true){
if ($CaoUser->vip_status()) {
$vipcate = false;
}
}
global $post;
$ratio = cao_thumbnail_ratio(); ?>
<div class=”entry-media”>
<div class=”placeholder” style=”padding-bottom: <?php echo esc_attr( $ratio ); ?>;”>
<?php if($vipcate) : ?>
<a<?php echo _target_blank();?> href=”<?php echo get_category_link($cateid); ?>”>
<?php else : ?>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_permalink() ); ?>”>
<?php endif; ?>
<img class=”lazyload” data-src=”<?php echo esc_url(_get_post_timthumb_src()); ?>” src=”data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==” alt=”<?php echo get_the_title(); ?>”>
</a>
<?php if (_cao(‘post_tim_rload’,’0′)) { ?>
<?php if($vipcate) : ?>
<a<?php echo _target_blank();?> href=”<?php echo get_category_link($cateid); ?>”><div class=”cao-cover”><img src=”<?php echo get_template_directory_uri();?>/assets/images/svg/rings.svg” width=”50″ height=”50px”></div>
<?php else : ?>
<a<?php echo _target_blank();?> href=”<?php echo esc_url( get_permalink() ); ?>”><div class=”cao-cover”><img src=”<?php echo get_template_directory_uri();?>/assets/images/svg/rings.svg” width=”50″ height=”50px”></div>
<?php endif; ?>
<?php } ?>
</div>
<?php get_template_part( ‘parts/entry-format’ ); ?>
</div>
<?php
}
第三步:打开主题category.php文件
在第一个<?php 之后插入以下代码:
global $current_user;
$CaoUser = new CaoUser($current_user->ID);
$vipcate = in_array($cat_ID,_cao(‘_vipcate’));
if($vipcate == true){
if ($CaoUser->vip_status()) {
$vipcate = false;
}
}
在 <div class=”container”> 之前插入 <?php if($vipcate==false) : ?>
在最后一个 <?php 之前、</div>之后插入
<?php else: ?><!–VIP分类–>
<div class=”vipcate”>
<p class=”vipcatetext”>当前分类为VIP专属分类,请开通会员后访问!</p>
<?php if(!is_user_logged_in()) : ?>
<div class=”vipcatebtn”><a href=”javascript:;” onclick=”open_signup_popup()”>立即登录开通会员</a></div>
<?php else: ?>
<div class=”vipcatebtn”><a href=”<?php echo home_url(“/user?action=vip”); ?>”>立即开通会员</a></div>
<?php endif; ?>
</div>
<?php endif;?>
第四步:打开主题\parts\template-parts\content-grid.php文件,
1.在 <?php 之后插入 global $current_user;
2.搜索 cao_entry_media(); 替换为 yite_entry_media(get_the_ID(),$current_user->ID);
3.搜索 cao_entry_header( array( ‘category’ => true ) ); 替换为 yite_entry_header( array( ‘category’ => true ) ,$current_user->ID);
至此修改完毕,在后台RIpro设置-高级功能-仅会员可见分类 设置相关分类保存即可!