如下图所示,游客以及非 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);
自定义 CSS 样式代码 内即可。
至此修改完毕,在后台 RIpro 设置-高级功能-仅会员可见分类 设置相关分类保存即可!
转载请注明:汇站网 » Ripro 美化-增强功能-增加 VIP 分类