Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was wondering if someone could explain how to roughly code this into php:

if catergory = Cars && user is unregistered
<my code>
else
<my code>
share|improve this question

1 Answer 1

up vote 1 down vote accepted
<?php

    $current_user = wp_get_current_user();

    if ( is_category( 'Cars' ) && 0 == $current_user->ID ) { ?>

<!-- HTML markup here -->

<?php } else { ?>

<!-- other HTML markup here -->

<?php } ?>

ID will be 0 in the $current_user object, if the user isn't logged in.

Related reading:

EDIT

Try using is_user_logged_in() and in_category() instead:

<?php
if ( in_category( 'cars' ) && ! is_user_logged_in() ) {
    // Current post is in the category 'cars'
    // and the current user is NOT logged in;
    // do something
} else {
    // do something else
}
?>
share|improve this answer
1  
Note: you could also use is_user_logged_in() in place of querying the current user. Might be a bit cleaner... –  Chip Bennett Nov 30 '11 at 14:57
    
Alright, so only half of the code is working for me. It doesn't kick in the code for category cars and not being logged in. It just skips right to the else. Any ideas? Does it matter if cars is a subcatergory? EDIT: I took out the "category( 'Cars' ) &&" and the code works if I'm logged out. So basically I narrowed down that it is the category code that isn't working properly. Suggestions? –  EGamerHDK Nov 30 '11 at 15:18
    
is_category takes the category id, slug or title as an argument. If it doesn't do it's job, your category's title is not "Cars". Btw, +1 for @Chip –  Johannes Pille Nov 30 '11 at 15:25
    
I took out the code "&& 0== $current_user->ID" and it still doesn't work. I created a new category named "test" and put it on a post and I also put "test" in the code and no luck. do you think it could be from my custom layout? Maybe I didn't call some Category thing? –  EGamerHDK Nov 30 '11 at 15:31
    
Feed it the category ID, just to be failsafe, but the conditional tag is_category works independent of your theme / layout. Are you sure you're testing this correctly? Can you put into words what it is you're trying to achieve? –  Johannes Pille Nov 30 '11 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.