• Resolved agnesvarda

    (@agnesvarda)


    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.

Viewing 1 replies (of 1 total)
  • 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()) : ?>
Viewing 1 replies (of 1 total)
  • The topic ‘BUTTON TO POSTS IN CHRONOLOGICAL ORDER’ is closed to new replies.