Hide categories and articles but see them after login - WordPress Tutorial

The code shared in this WordPress tutorial allows visitors to hide articles under the specified category without logging in, and the URL of the article visited by the user who is not logged in will return 404

Some users want to show some content only to the login users of the website, so that they can cooperate with Invitation code They charged a fee. this time Wordpress Tutorial The shared code can If the visitor is not logged in, the article under the specified category will be hidden, and the URL of the article visited by the user who is not logged in will return 404

Add the following code to the functions.php In <? php Then:

 ob_start(); function yincangwenzhang( $query ) { $delete_cat_yc=array('-72','-4','-5');// The upper and lower numbers should always represent your classified ID $delete_cat=array('72','4','5'); if(! is_user_logged_in()){ //Jump 404 when the user is not logged in to visit the article page if(is_single()&&in_category($delete_cat)){ wp_safe_redirect(home_url().'/ 404'); exit; } //Hide articles on the home page and other pages //$query->is_home()||$query->is_search()||$query->is_archive() //The uplink code works the same way, but I don't know why the articles on the author's page are not hidden, and it is useless to have parameters, so use the following If (get_current_user_id()==0) {//The user's unlisted ID is 0, and the article is hidden $query->set( 'cat', $delete_cat_yc); } }return $query; } add_filter( 'pre_get_posts', 'yincangwenzhang' );

Prompt: get_current_user_id(); The function is to obtain the user ID

Related recommendations

隐藏分类以及文章但登陆后可见 - WordPress教程-Npcink
隐藏分类以及文章但登陆后可见 - WordPress教程-Npcink

Wordpress hides part of the content of the article, which needs to be visible after registering on the website

隐藏分类以及文章但登陆后可见 - WordPress教程-Npcink
隐藏分类以及文章但登陆后可见 - WordPress教程-Npcink

How can I hide part of the article content and make it visible after comments- Wordpress development

The bottom block of Youyou Tutorial Network - WordPress block

2020-9-3 15:16:41

Foreign themes

Dropout - Creative multi-purpose wordpress theme

2020-2-24 7:48:29

⚠️
Some codes and tutorials on Npcink come from the Internet, and are only for netizens to learn and exchange. If you like this article, you can Attach original link Reprint at will.
No intention of infringing your rights, please send an email to 1355471563#qq.com Or click on the right Private message: Muze feedback, we will deal with it as soon as possible.
4 replies A Author M administrators
  1.  Xi Shi

    Why is my reply missing.

    •  Muze

      Look again

  2.  odmins

    If there is a category menu in the menu navigation, there will be blank categories without articles. If you can click categories, you will also be prompted to log in or jump to the specified interface, which will be perfect

    •  Muze

      Theoretically, it can be achieved, but it needs to use your intelligence

Personal Center
Shopping Cart
Coupon
Sign in today
There are new private messages Private Message List
search