• Resolved Frances Allen

    (@frances-allen)


    A year ago I was kindly given the solution:

    <?php
    
    // 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;
    }
    
    ?>

    Since then I have tried to improve my blog archive page and clearly have messed something up. If I put in ‘order’ , ‘asc’ in myfunctions file , it reverses the order of my current blog as well as the archives.

    I use the chunk theme and have created a chunk-child folder. Then…. without really knowing what I was doing!…. I have created a blog-archive page using Custom Archives, whichrefers to a file page_archives_custom.php. I have also created two files fullwidthtemplate.php and contentfullwidth.php, which I think work with this custom archive page (don’t ask me how!). And finally a functions file, in which I put the code kindy provided by Kathryn.

    As you can see, I’m out of my depth, and would really appreciate it if someone could help me.

    (I have a secondary plea for help,which I imagine I should post separately – to get next and previous links to work!)

Viewing 6 replies - 1 through 6 (of 6 total)
  • try and change the code to:

    // 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;
    }
    Thread Starter Frances Allen

    (@frances-allen)

    Thiis came up with the message
    Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /home/francesa/public_html/wp/wp-content/themes/chunk-child/functions.php on line 6

    have you managed to get back into the dashboard, in case the error prevented you to do so?

    generally, the suggested code seems ok;

    however, I hope you have kept the php tags from your posted code ??

    what is the current full code of functions.php right now?

    Moderator keesiemeijer

    (@keesiemeijer)

    Hi,

    Try it with this:

    // Runs before the posts are fetched
    add_action( 'pre_get_posts' , 'my_change_order' );
    
    // Function accepting current query
    function my_change_order( $query ) {
    	// Check if the query is for an archive
    	// And it's the main query
    	if ( is_archive() && $query->is_main_query() ) {
    		// Query was for archive, then set order
    		$query->set( 'order' , 'ASC' );
    	}
    	// This is an action, no need to return anything
    }

    The pre_get_post hook is an action (not a filter) where you can use the conditional functions like is_archive() for the main query.

    Thread Starter Frances Allen

    (@frances-allen)

    I think I have found the culprit, without understanding why. The original version of functions also had the following lines:

    add_action('parse_query', 'no_nopaging');

    I removed these and all seems to be OK.

    Thank you for taking the time to tryto help me

    Thread Starter Frances Allen

    (@frances-allen)

    So functions is now:

    <?php
    
    // 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;
    }
    
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Make archive posts ascending revisited’ is closed to new replies.