Order posts by date ascending in a particular category (4 posts)

  1. gt112
    Posted 8 years ago #

    Hi all.

    I have searched long and hard on here for topics relating to sorting. I have read about editing the index.php fiel of a particular template and placing a sort command just befor eth eloop etc and I now understand all that.
    Here's my question.
    Can I sort all the posts in ONE PARTICULAR Category by data ASC - showing the oldest post first and newest post last etc. I only want to do this for posts in 1 category. The posts in all other categories should remain as they are.
    Can anyone please help?

  2. gt112
    Posted 8 years ago #

    I have had a go at solving thi smyself and it seems to work.

    Here's what I did

    1. Go to the template folder for the active wp templete

    2. Copy index.php to a file called category-n.php (where n is the number of the category you wish to sort in). You can find this number by looking at the terms table in phpMyAdmin. In my case the category was 9 so I created a file called category-9.php

    3. Edit category-n.php

    4. just before the loop <?php while (have_posts()) : the_post(); ?>)
    place this line of code
    <?php query_posts($query_string."&orderby=date&order=ASC"); ?>

    5. Edit the cosmetic navigation page links from “Previous Posts” to “Further Posts” as follows. Change the line
    <div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
    <div class="alignright"><?php next_posts_link('Further Entries &raquo;') ?></div>
    6. Save the file and upload it to the template folder on your server.

  3. gt112
    Posted 8 years ago #

    A further refinement (for some reason the edit link has dissapeared so I have to submit a new post)

    Point 5 above should now be read as follows

    5. Edit the cosmetic navigation page links from “Previous Posts” to “Further Posts” as follows. Change the lines

    <div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>


    <div class="alignright"><?php next_posts_link('Further Entries &raquo;') ?></div>
    <div class="alignleft"><?php previous_posts_link('&laquo; Previous Entries ') ?></div>
  4. TomJohnson
    Posted 7 years ago #

    Hey, that code works great for sorting posts in ascending order. Thanks for posting it.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.