文章目录[隐藏]

前言说明

在我们使用WordPress发布文章时,往往需要隐藏内容功能,有的主题可能自带了,但有的主题却没有这个功能,特别是资源站,这个功能特别重要,无论是为了内容收费还是单独为了隐藏内容给会员用户等等,都是增加用户粘性的一种方法,下面我们就介绍几种简单的隐藏方法,操作简单,使用起来也很方便,当然你也可以根据这个思路增加自己的特殊功能。

如何隐藏WordPress文章内容教程说明插图

代码内容

在主题文件夹下的“function.php”中添加代码后,在编辑文章时使用相关的短码即可

只限游客访问的内容

1.在“function.php”文件添加如下代码

add_shortcode( 'ie_guest', 'ie_guest_sc' );
function ie_guest_sc( $atts, $content = null ) {
    extract( shortcode_atts( array(
               'in' => '',
               'admin' => '',
    ), $atts ) );
    $in = str_replace(' ', '', $in);
    $includeds = explode(",", $in);

    $user_id = get_current_user_id();

    //check if user is logged in
    if ( is_user_logged_in() ) {
        //check if admin is allowed
        if ($admin === '1') {
            //loop through included user ids
            foreach ($includeds as $included) {
                //check if user is included
                if ($user_id == $included) {
                    return do_shortcode($content);
                }
            }
            //check if user is admin
            if ( current_user_can('administrator') ) {
                return do_shortcode($content);
            } else {
                return '';
            }
        } else {
            //loop though included user ids
            foreach ($includeds as $included) {
                //check if user is included
                if ($user_id == $included) {
                    return do_shortcode($content);
                }
            }
            return '';
        }
    //show content to guests
    } else {
        return do_shortcode($content);
    }
}

2.发布文章时调用代码如下:

[ie_guest] 这里是隐藏内容[/ie_guest]

只限登录用户访问的内容

1.在“function.php”文件添加如下代码

add_shortcode( 'ie_loggedin', 'ie_loggedin_sc' );
function ie_loggedin_sc( $atts, $content = null ) {
    extract( shortcode_atts( array(
      'ex' => '',
    ), $atts ) );

    $ex = str_replace(' ', '', $ex);
    $excludeds = explode(",", $ex);

    $user_id = get_current_user_id();

    //check if user is logged in
    if ( is_user_logged_in() ) {
        //loop through excluded user ids
        foreach ($excludeds as $excluded) {
            //check if user is excluded
            if ($user_id == $excluded) {
                //show nothing
                return '';
            }
        }
        //show content to logged in users
        return do_shortcode($content);
    //hide content to guests
    } else {
        return '';
    }
}

2.发布文章时调用代码如下:

[ie_loggedin] 这里是隐藏内容[/ie_loggedin]

只限特定用户组用户访问

1.在“function.php”文件添加如下代码

add_shortcode( 'ie_role', 'ie_role_sc' );
function ie_role_sc( $atts, $content = null ) {
  
  extract( shortcode_atts( array(
    'roles' => '',
    'inverse' => '',
  ), $atts ) );

  if (!is_user_logged_in()) { return; }

  $roles = str_replace(' ', '', $roles);
  $role_array = explode(",", $roles);

  $user = wp_get_current_user();
  $user_roles = ( array ) $user->roles;

  //loop through allowed roles
  if ($inverse !== '1') {
    foreach($user_roles as $user_role) {
      foreach ($role_array as $allowed_role) {
        //check if user has allowed role
        if ($user_role == $allowed_role) {
          //show nothing
          return do_shortcode($content);
        }
      }
    }
  } else {
    $role_found = 0;
    foreach($user_roles as $user_role) {
      foreach ($role_array as $allowed_role) {
        //check if user has allowed role
        if ($user_role == $allowed_role) {
          //show nothing
          $role_found = 1;
        }
      }
    }
    if (!$role_found) {
      return do_shortcode($content);
    }
  }
  
  return;

}

2.发布文章时调用代码如下:

[ie_role roles="administrator, editor"] 这里是隐藏内容[/ie_role]

说明:这里的“roles”指的是您的用户组!

发表回复

后才能评论