简体   繁体   English

WordPress-通过单击更改顺序

[英]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. 考虑以下代码,我在Wordpress博客中有一个循环,该循环将按从旧到新的顺序对帖子进行排序。

<?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. 我曾考虑过使用jQuery实现此目的,但我不知道如何根据用户单击的链接来更改PHP代码。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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