Display posts in alphabetical order for a particular category

I`m looking for a solution to dispaly posts in alpahbetical order if category is “Glossary” otherwise display the posts in ascending order. When I tried the following code its works perfect for category Glossary,but the order of other posts went wrong.

<?php global $query_string; 

if(is_category('Glossary'))
{
query_posts($query_string . "&orderby=title&order=ASC");
while ( have_posts() ) : the_post();
the_title();
endwhile; 




}
else{
query_posts($query_string . "&order = ASC"); ?>
             <?php while ( have_posts() ) : the_post(); ?>
                    <?php  the_title(); ?>

                <?php endwhile; ?>
<?php } ?>

Hope someone will help me to solve the problem.

Related posts

1 comment

  1. First, I would solve your problem using WPQuery. Best to go there as opposed to query_posts

    Second, once you acknowledge and accept that WP Query is your friend for as long as you are within the wonderful world of WP theming, then you have to do as follows to solve your particular pickle:

    if(is_category('your_category') :
        $args = array(
            'post_type'         => 'post',  
            'posts_per_page'    => 'how_many_posts_you_want_-1_if_all',
            'cat'               => 'your_category_number',
            'orderby'           => 'title', 
            'order'             => 'ASC'
        );
    else :
        $args = array(
            'post_type'         => 'post',  
            'posts_per_page'    => 'how_many_posts_you_want_-1_if_all',
            'order'             => 'ASC'
        );
    endif;
    $loop = new WP_Query( $args ); 
    while($loop->have_posts()) : $loop->the_post(); 
        //do your magic here
    endwhile;
    wp_reset_query();
    

    And that ought to do it. You could optimize the $args array a bit as I placed some items on both conditionals, but that can do it for a quick test.

Comments are closed.