搜索
首页CMS教程WordPressWordPress如何只允许游客浏览指定分类的文章

下面由WordPress教程栏目给大家介绍WordPress只允许游客浏览指定分类的文章的设置方法,希望对需要的朋友有所帮助!

WordPress如何只允许游客浏览指定分类的文章

有童鞋问,如何实现未登录用户只能浏览指定的分类文章,这里提供一下方法。

WordPress只允许游客浏览指定分类的文章只允许游客浏览指定分类的文章

网上可以找到类似的代码:

//template_redirect动作钩子是一定会执行的,所以用这个钩子对全站有效
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
//判断登录,about页面就允许访问
if( !is_page('about') && !is_user_logged_in() ){
auth_redirect(); //跳转到登录页面
exit();
}
}

代码源自:www.ashuwp.com

原代码只允许未登录者访问指定的页面或者直接跳转到登录。

简单改一下,加个判断in_category,只允许访问指定分类的归档页面和文章。

// 首页和指定分类文章可以访问
add_action( 'template_redirect', 'ashuwp_show_only_login', 0 );
function ashuwp_show_only_login(){
//判断登录,只允许访问ID为3和2的分类文章
if( !in_category( array( 3,2 ) ) && !is_home() && !is_user_logged_in() ){
auth_redirect(); //跳转到登录页面
exit();
}
}

默认未登录者只允许访问网站首页和分类id为3和2的分类归档页面和文章,否则跳转到登录页面。

把 !in_category 前面的感叹号去掉改成 in_category,则正好相反,访问分类ID为3和2的分类文章跳转到登录,其它文章可以正常访问。

代码添加到主题函数模板functions.php中。

以上是WordPress如何只允许游客浏览指定分类的文章的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:zmingcx。如有侵权,请联系admin@php.cn删除
使用WordPress管理内容有多容易?使用WordPress管理内容有多容易?May 09, 2025 am 12:11 AM

WordPressiser-FrightlyDuetLoitsIntuitiveInterfaceAndcms,whosparateContentFromDesign.itoffersArichTextedextEditorforeasyContentCreationAndialibraryFororRaryFororRory.itsflexeNhangedBancedBynhangedBynHangedBynumereNumerSandeMesandPlugins,elloverSandplugins,elloverOverUseCanimpActpercrance

WordPress如何在业务环境中使用?WordPress如何在业务环境中使用?May 08, 2025 am 12:04 AM

1)ITSUPPORTSE-CommerceWithPluginSlikeWooCommerce,允许ProductManagementAndAndPaymentProcessing.2)ITSERVESASASASACMSACMSACMSMSACMSMSACMSMASTRATATEBLOGS,增强SeoAndEngagement.3)

哪些类型的网站不适合WordPress?哪些类型的网站不适合WordPress?May 07, 2025 am 12:10 AM

WordPressIsnotIdeAlforHigh-TrafficWebsites,customandCompleXapplications,Security-SensitiveApplications,Real-TimedataProcessing,AndhighlyCustomizedUserInterfaces.forhigh-Trafficsites,USENENEXT.JSORXOLCT.JSORXORX.JSORCUSTOMSOMOLTICTS; forCompleXapplications; forcomplexapplications; optfordjangoorrub

您可以使用WordPress构建博客吗?您可以使用WordPress构建博客吗?May 06, 2025 am 12:03 AM

Yes,youcanbuildablogwithWordPress.1)ChoosebetweenWordPress.comforbeginnersorWordPress.orgformorecontrol.2)Selectathemetopersonalizeyourblog'slook.3)Usepluginstoenhancefunctionality,likeSEOandsocialmediaintegration.4)Customizeyourthemewithsimplecodetw

WordPress作为CMS平台的安全程度如何?WordPress作为CMS平台的安全程度如何?May 05, 2025 am 12:01 AM

WordPressCanbeseCureifManagedProperly.1)keepthewordPressCoreUpdatedTopatchVulnerabilities.2)vetandupdatepluginsandthemesfromreputables.3)EnforcestrongpasseTSandusetWordssandusetWordwordwo-factorauthenticaliation.4)

您可以使用WordPress CMS构建哪种网站?您可以使用WordPress CMS构建哪种网站?May 04, 2025 am 12:06 AM

WordPressCanbuildVariousTypesofwebsites:1)个人博客,EasyTosetUpWithTheMesandPlugins.2)BusinessWebsites,使用drag-and-dropbuilders.3)e-commercePlatforms,forwoocommerceforsemcommerceforseameamseamelesssites.4)communitySites.4)conduction.4)使用bbudicatipration

将WordPress用作CMS的优缺点是什么?将WordPress用作CMS的优缺点是什么?May 03, 2025 am 12:09 AM

WordPressisapowerfulCMSwithsignificantadvantagesandchallenges.1)It'suser-friendlyandcustomizable,idealforbeginners.2)Itsflexibilitycanleadtositebloatandsecurityissuesifnotmanagedproperly.3)Regularupdatesandperformanceoptimizationsarenecessarytomainta

WordPress与其他流行的CMS平台相比如何?WordPress与其他流行的CMS平台相比如何?May 02, 2025 am 12:18 AM

WordPressExcccelineaseeandaDaptability,MakeitiTidealForBeginnersandsMallTomedium-SizedBusinesses.1)siseofuse:wordpressisuser-Frylyly.2)安全:drupalleadswithstrongsecurityfeatures.3)性能:performance:performance formation:ghandoffersefersefersefersefersefersefersefersexcellentperformanceeduetonodeutonode.jsorscor.jssor.jjsy.jjsy.jjsy.4)4)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用