WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] have_posts() ASC and DESC (5 posts)

  1. kharr212
    Member
    Posted 4 years ago #

    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?

  2. MichaelH
    Member
    Posted 4 years ago #

    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 ?>
  3. kharr212
    Member
    Posted 4 years ago #

    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!

  4. MichaelH
    Member
    Posted 4 years ago #

    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).

  5. kharr212
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic