using the category ID, try
is_category(5)
or use the cat slug
is_category('what-ever-cat')
Make sure you are following the order of using query_posts. is_category() can be used before you run query_posts(). That way, you can alter the main query properly.
Here is a basic example:
<?php
// first detect the category
if ( is_category(5) ) {
$args = 'p=644';
} elseif ( is_category(6) ) {
$args = 'p=645';
} else {
// $args = something else or default action...
}
// next alter the main query
query_posts( $args );
// now start your loop
if ( have_posts() )... etc..
Hello,
Thank you both for the reply!
I have tried racer x’s (btw, Paul Gilbert is a god) code condensed as:
<?php
// first detect the category
if ( is_category(5) ) {
$args = 'p=644';
}
// next alter the main query
query_posts( $args );
?>
And I am still getting a:
Parse error: syntax error, unexpected T_ELSE
<?php if ( have_posts() && is_category('5') ) : query_posts( 'p=644' ); ?>
Still works fine, so I am confused as hell!
When I said “etc.” I should have clarified that you just use the regular start of a loop right after you use query_posts( $args ).
if (have_posts()) : while (have_posts()) : the_post();
When you are using query_posts you are altering the MAIN loop for the page. So there is no need for “have_posts() && is_category(‘5’) )”.
BTW – I actually never heard of Paul Gilbert, nor knew about a band called Racer X! Ha! Even though I have played guitar professionally at gigs for years. Racer X was from a childhood cartoon I watched called Speed Racer.
Ha I see! I suppose every time I head Racer X I just think of the band.
Regarding the code, I tried adding the if (have_Posts()) : to the front of my loop (before it was just while (have_posts()) : the_posts(); ), and I no longer get a parse error, but now it shows every single post whenever it does not enter a conditional!
Is there a way to null my $args to show no posts? I am thinking I just need to plug that into my else statement.
I actually tried changing $args to an arbitrary variable ($posts), but the query still uses all posts!
Thanks!
When you are using both if(have_posts()) and while (have_posts()) then you need to make sure you are closing BOTH the “if” and “while” properly.
if (have_posts()) : while (have_posts()) : the_post();
// do stuff
endwhile; endif;
If you are just using while (have_posts()) then you only need the closing “endwhile;”.
If you don’t want a loop to run as a default if the specified categories are not found, then just bypass the whole thing like so:
<?php
if ( is_category(5) ) {
$args = 'p=644';
} elseif ( is_category(6) ) {
$args = 'p=645';
}
if ( $args ) {
query_posts( $args );
if (have_posts()) : while (have_posts()) : the_post();
// do your post loop
endwhile;
else :
// sorry, no posts found
endif;
} // end if $args were found
Although, I am not fully understanding the logic here because this means you have an entire category(s) that will only display one post if queried? That does not sound like a use for categories then which are for a “collection” of posts. I may be misunderstanding your situation though.
Perhaps using a category template would be better?
PS – I’ll check out the band anyway!
Hi Racer X,
Thanks for the reply! You are right; my decision to show one post per category was a poor decision on taxonomy (hard explain unless you saw the nature of the site). I have decided just to use a custom post template to achieve what I was wanted.
However, your explanations taught me about WordPress syntax so it was much appreciated.
Thanks for your time!
Btw, I hope you like 80’s speed metal!
I figured it was one of those “situations” with taxonomies or whatever. I fall into that as well.
I love 80’s rock! That’s the era I grew up in. I was more standard metal than speed metal though (VH, Rush, etc.).
Best of luck!