WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort Posts in ascending order for Archive listing only (6 posts)

  1. Lozboz
    Member
    Posted 4 years ago #

    Hi, I'm using Freshy theme by Jide. 809 on my photo 365 project blog

    I would like to change the 'newest first' sort order for the month archive listings.
    I know that by default the theme post display order is set to descending order and we can change it by adding below mentioned code in index.php file of current active theme. However, this changes all display post orders.

    Can it be achieved selectively?

    Thanks

    Laurie

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    I know that by default the theme post display order is set to descending order and we can change it by adding below mentioned code in index.php file of current active theme.

    Did you forget something? ;-) ... *hint* the code you mentioned.

    To answer the question though, yes you can change query parameters selectively in different situations..

    My question now is, does your theme have an archive.php file, and how capable are you with code, do you need an example or just some guidance on what functions to call etc... ?

  3. Lozboz
    Member
    Posted 4 years ago #

    Hi,

    Thanks for the quick response.

    Yes, I missed that the code I was copying was an image. I meant to quote <?php query_posts('order=ASC'); ?>

    Yes, my theme has an archives.php and I can edit it.

    I am new to this code but quite capable in other languages, so I'm happy to learn! I could certainly do with some assistance in identifying functions etc.

    Thanks

    Laurie

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    Does your theme have an archive.php file? If so, make your changes to that template file. If not, make a copy of index.php and name the new copy "archive.php. Then make your changes.

    If you want to retain pagination, you might also want to extend the query to:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'order' => 'ASC',
    	'paged' => $paged
    );
    query_posts($args);
    ?>
  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Alternatively, function..

    // Runs before the posts are fetched
    add_filter( 'pre_get_posts' , 'my_change_order' );
    // Function accepting current query
    function my_change_order( $query ) {
    	// Check if the query is for an archive
    	if($query->is_archive)
    		// Query was for archive, then set order
    		$query->set( 'order' , 'asc' );
    	// Return the query (else there's no more query, oops!)
    	return $query;
    }

    in the theme's functions.php ... :)

  6. Lozboz
    Member
    Posted 4 years ago #

    Thank you very much, it worked a treat!

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic