简体   繁体   中英

Wordpress - change order by on click

Considering the following code, I have the loop in my Wordpress blog which will order posts from oldest to newer.

<?php
    $args = array(
        'order'    => 'ASC'
    );
    query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part('content'); ?> 
<?php endwhile; ?> 

What I want is to create two links on which the user clicks and changes this parameter, from older to newer or newer to older.

I thought about using jQuery to achieve this but I don't know exactly how I'll change the PHP code based on which link the user clicks.

If you change your sort direction to a parameter, eg

<?php
        $args = array(
            'order'    => (isset($_GET['dir']) ? $_GET['dir'] : 'ASC')
        );
        query_posts( $args );
    ?>

then you can create a link like:

<a href="http:example.com/yourpage.php?dir=DESC">Newest to Oldest</a>
<a href="http:example.com/yourpage.php?dir=ASC">Oldest to Newest</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM