WordPress.org

Ready to get started?Download WordPress

Forums

Querying posts based on category (9 posts)

  1. ericjohnson
    Member
    Posted 1 year ago #

    Hi,

    I need to query individual posts for different category pages.

    I have the code:

    <?php if ( have_posts() && is_category('5') ) : query_posts( 'p=644' ); ?>

    But I need it to have nested if statements to check for each category. I have tried the if statement with the inline with the following code:

    <?php
    if ( have_posts() ) {
    query_posts( 'p=644' );
    }
    ?>

    I am getting an error with this basic code, and I haven't even begun to nest if statements yet!

    I wanted it to look something like:

    <?php
    if ( have_posts() ) {
    if ( is_category('5') ) {
    query_posts( 'p=644' );
    } elseif ( is_category('6') ) {
    query_posts('p=645' );
    }
    }
    ?>

    Can someone please explain why the code above does not work, or suggest a better alternative to nesting it?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    using the category ID, try

    is_category(5)

    or use the cat slug

    is_category('what-ever-cat')

  3. racer x
    Member
    Posted 1 year ago #

    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..
  4. ericjohnson
    Member
    Posted 1 year ago #

    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!

  5. racer x
    Member
    Posted 1 year ago #

    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.

  6. ericjohnson
    Member
    Posted 1 year ago #

    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!

  7. racer x
    Member
    Posted 1 year ago #

    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!

  8. ericjohnson
    Member
    Posted 1 year ago #

    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!

  9. racer x
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.