• My requirement is to change the display order of the post in categories from latest (default) to oldest as the first post.

    For that, I got a solution which is given below:-

    You don’t need a plugin for that. Just take a look over here: https://codex.www.ads-software.com/Function_Reference/query_posts#Orderby_Parameters

    orderby=date & order=DESC

    I am very much new to wordpress and could not really understand the content of this link above.

    Can u please guide me on the location and the file which I need to change for this as I have no clues on php coding.

Viewing 4 replies - 1 through 4 (of 4 total)
  • well first you have to determine what displays your categories…. in your theme its category.php if you have one, otherwise its archive.php

    if its category.php you are all set, just add the code. If its archive.php you have something to think about. If you adjust the post order of archive.php, it will affect all archives that are displayed by that template. Possibly date based, author based, category based, tag based, etc.

    If you are cool with that then you can add the code to archive.php. If not, you’ll need to make a category.php. This could be as simple as making a copy of archive.php and renaming it category.php.

    The code itself is this:

    <?php query_posts( array(
    		// Set query
    		'orderby' => 'date',
                      'order' => 'DESC',
    		// Enabled paging
    		'paged'=> ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    	)); ?>

    it goes right above your loop…. which starts something like

    <?php if (have_posts()) : ?>

    Thread Starter pshah

    (@pshah)

    Hi Rev Voodoo,

    Thanks for your repsponse.
    I have a category.php at the following path:-
    C:\Inetpub\wwwroot\wordpress\wp-content\themes\twentyten

    I do not see anything like <?php if (have_posts()) : ?> in the same.
    The code for the same is:-

    <?php
    /**
    * The template for displaying Category Archive pages.
    *
    * @package WordPress
    * @subpackage Twenty_Ten
    * @since Twenty Ten 1.0
    */

    get_header(); ?>

    <div id=”container”>
    <div id=”content” role=”main”>

    <h1 class=”page-title”><?php
    printf( __( ‘Category Archives: %s’, ‘twentyten’ ), ‘<span>’ . single_cat_title( ”, false ) . ‘</span>’ );
    ?></h1>
    <?php
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo ‘<div class=”archive-meta”>’ . $category_description . ‘</div>’;

    /* Run the loop for the category page to output the posts.
    * If you want to overload this in a child theme then include a file
    * called loop-category.php and that will be used instead.
    */
    get_template_part( ‘loop’, ‘category’ );
    ?>

    </div><!– #content –>
    </div><!– #container –>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Please let me know on where should I put the code that you have mentioned.

    Thanks.

    Thread Starter pshah

    (@pshah)

    Even the archive.php looks something like this:-

    <?php
    /**
    * The template for displaying Archive pages.
    *
    * Used to display archive-type pages if nothing more specific matches a query.
    * For example, puts together date-based pages if no date.php file exists.
    *
    * Learn more: https://codex.www.ads-software.com/Template_Hierarchy
    *
    * @package WordPress
    * @subpackage Twenty_Ten
    * @since Twenty Ten 1.0
    */

    get_header(); ?>

    <div id=”container”>
    <div id=”content” role=”main”>

    <?php query_posts( array(
    // Set query
    ‘orderby’ => ‘date’,
    ‘order’ => ‘DESC’,
    // Enabled paging
    ‘paged’=> ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 ),
    )); ?>

    <?php
    /* Queue the first post, that way we know
    * what date we’re dealing with (if that is the case).
    *
    * We reset this later so we can run the loop
    * properly with a call to rewind_posts().
    */
    if ( have_posts() )
    the_post();
    ?>

    <h1 class=”page-title”>
    <?php if ( is_day() ) : ?>
    <?php printf( __( ‘Daily Archives: <span>%s</span>’, ‘twentyten’ ), get_the_date() ); ?>
    <?php elseif ( is_month() ) : ?>
    <?php printf( __( ‘Monthly Archives: <span>%s</span>’, ‘twentyten’ ), get_the_date(‘F Y’) ); ?>
    <?php elseif ( is_year() ) : ?>
    <?php printf( __( ‘Yearly Archives: <span>%s</span>’, ‘twentyten’ ), get_the_date(‘Y’) ); ?>
    <?php else : ?>
    <?php _e( ‘Blog Archives’, ‘twentyten’ ); ?>
    <?php endif; ?>
    </h1>

    <?php
    /* Since we called the_post() above, we need to
    * rewind the loop back to the beginning that way
    * we can run the loop properly, in full.
    */
    rewind_posts();

    /* Run the loop for the archives page to output the posts.
    * If you want to overload this in a child theme then include a file
    * called loop-archives.php and that will be used instead.
    */
    get_template_part( ‘loop’, ‘archive’ );
    ?>

    </div><!– #content –>
    </div><!– #container –>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I added the code that is mentioned in bold above. But this too had no impact on the order of the posts. It is same as latest at the top and oldest at the bottom. My requirement is excat opposiute of it

    Thread Starter pshah

    (@pshah)

    Hi Rev Voodoo,

    Please guide me in this regard.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘changing the post order, writing query_posts()’ is closed to new replies.