WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] BUTTON TO POSTS IN CHRONOLOGICAL ORDER (2 posts)

  1. agnesvarda
    Member
    Posted 3 years ago #

    I would like to have a button on my home page to switch my posts to cronological order. I was able to change

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

    to

    <?php
    
    query_posts($query_string . '&order=ASC');
    if (have_posts()) : ?>

    and it works fine. But how to actually make a button that on click reverses the loop order? I would like to be able to switch back and forth from order=ASC to order=DESC.

    thank you.

  2. ambrosite
    Member
    Posted 3 years ago #

    Put this PHP code at the very top of your template:

    <?php
    $post_order = 'ASC';
    if ( isset($_POST['last_order']) && $_POST['last_order'] == 'ASC' ) {
    	$post_order = 'DESC';
    } ?>

    Now put this HTML code to create the button somewhere lower in your template (wherever you want it to appear):

    <form method="post" action="#">
    <input type="hidden" name="last_order" value="<?php echo $post_order; ?>">
    <input type="submit" value="Reverse Post Order">
    </form>

    Finally, modify your loop code as follows (notice the replacement of the single quotes with double quotes, that is important):

    <?php
    query_posts($query_string . "&order=$post_order");
    if (have_posts()) : ?>

Topic Closed

This topic has been closed to new replies.

About this Topic