Grow your CSS skills. Land your dream job.

WordPress hide categories from logged out users

  • # May 7, 2013 at 12:06 pm

    Hi all.

    My client has a blog area in their site. They have two main categories, ‘dealer blogs’ and ‘public blogs’. Ideally, we want both logged in and logged out users to be able to go to the same page and see rather a) just public blogs, or b) both public, and ‘dealer only’ blogs.

    I have installed the plugin ‘user specific content’ and this seems to do what we need it to for the most part, however the plugin does NOT hide the blog completely, just shows a message saying ‘you must be logged in to view this content’. Also, the featured image still shows regardless of being logged in/out.

    the link: [http://www.sunspacemodular.com/blog/](http://www.sunspacemodular.com/blog/ “http://www.sunspacemodular.com/blog/”)

    If we could hide the ‘dealer’ category entirely when not logged in this would save all the hassle.

    Please keep in mind that we are using a purchased theme, and would like to avoid directly changing their code. We do have access to our own custom JS, CSS, and functions files.

    Thank you very much, and I apologize for the long post.
    Justin

    # May 7, 2013 at 12:23 pm

    You shouldn’t really need a plugin for this. You just need to use this:

    < ?php if ( is_user_logged_in() ) { ... } ?>

    Resource: http://codex.wordpress.org/Function_Reference/is_user_logged_in

    Inside your `is_user_logged_in` conditional, you can run the loop for your dealer blogs. Just have to make sure that you also use it on the actual single pages so that logged out people can’t see the single posts, either.

    So you’d do something like:

    < ?php if ( !is_user_logged_in() && is_category( 'Dealer Blog' ) ) {
    echo ‘Sorry, you need to be logged in to view this post. Log in.‘;
    } ?>

    # May 7, 2013 at 12:46 pm

    @TheDoc, This is exactly what I would do in a normal scenario, but I don’t want to edit the themes files. I am still relatively new with custom functions, is there a way to hook into the blog page with a custom function to accomplish this?

    I just don’t want my client to run an update, and my changes be overwritten.

    Thanks
    Justin

    # May 7, 2013 at 12:51 pm

    I’m not sure I understand, what theme are you using that you think is going to overwritten?

    # May 7, 2013 at 3:08 pm

    The theme I am using is [http://theme-fusion.com/avada/](http://theme-fusion.com/avada/ “http://theme-fusion.com/avada/”)

    My understanding of theme’s is that if you edit the core theme files and there is an update, the update will override my changed UNLESS it is in the custom_css/js/function files.

    Thanks

    # May 7, 2013 at 3:10 pm

    @TheDoc isn’t talking about changing the core files but rather the theme.

    Core files will be overwritten but theme files will not.

    Therefore, anything inside the themes folder will not be overwritten. Hope that clears things up for you.

    # May 7, 2013 at 3:24 pm

    Although, you *can* get theme updates as well. I think what people normally do here is create a child theme that has all of their changes in it, but I’ve never done that before.

    # May 7, 2013 at 9:18 pm

    You could use a conditional to remove the dealer category out of the loop using pre_get_posts for users that are not logged in. http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    Using pre_get_posts will allow you to do it as a function instead of modifying the template/theme files directly.

    And yes, theme files for WordPress are overwritten on updates (unless they add in that functionality for custom pages). Too many people neglect using child themes, which are awesome. :)

    # May 8, 2013 at 9:22 am

    Beautiful, thanks for the clarification everybody.

    I must look into child themes!
    @scottnix, pre_get_posts looks like the perfect solution, thanks!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".