WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] have_posts() ASC and DESC

[Resolved] have_posts() ASC and DESC

  • I have multiple categories. Some categories I would like to display ascending and others descending. Right now, I know how to make everything either ascending or descending. But am having a tough time figuring out how to add both. The logic is something like this:

    <?php if (is_category('pastevents,photos')) {; ?>
    <?php query_posts($query_string . "&order=ASC"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    content stuff goes here
    <?php endwhile; ?>
    
    }elseif( is_category() ) {
    
    <?php query_posts($query_string . "&order=DESC"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    content stuff goes here
    <?php endwhile; ?>
    }
    <?php endif ?>

    Am I getting close?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Looks like this is a category template…couldn’t it be this:

    <?php if (is_category('pastevents,photos')) {; ?>
    query_posts($query_string . '&order=ASC');
    }
    ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    content stuff goes here
    <?php endwhile; ?>
    }
    <?php endif ?>

    Michael, thanks for the help. But either I’m still missing something or the code above does not work. This is actually an archives template if that even matters. If I use your code above, then ALL categories are displayed ASC.

    Just so we both understand, the logic is such

    If category is pastevents or photos
    display in ascending order
    
    If any other category
    display in descending order

    Besides your code, I have also tried

    <?php if (is_category('pastevents,photos')) {; ?>
    <?php query_posts($query_string . "&order=ASC"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    asc order
    <?php endwhile; ?>
    
    <?php }elseif( is_category() ) { ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    desc order
    <?php endwhile; ?>
    <?php } ?>
    <?php endif ?>

    Anymore ideas? It does not seem that tough, except for the fact that I’m a noob and am still trying to learn the syntax. Thanks!

    Okay, using the WordPress Default theme, and the file wp-content/themes/default/archive.php, right before this line:

    <div class="navigation">

    add this:

    <?php
    if ( is_category(array('pastevents','photos')) ) {
    query_posts($query_string . '&order=ASC');
    }
    ?>

    Essentially that says, for the categories, pastevents and photos, sort the post in ascending date order, otherwise leave the sort alone (that sort will be in descending order by date).

    Thanks Michael, got it! I think the key was that the category is an array. Everything works now. Thanks again for your help!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] have_posts() ASC and DESC’ is closed to new replies.
Skip to toolbar