考虑到以下代码,我在Wordpress博客中有一个循环,它将按最旧到更新的顺序排列帖子。
<?php
$args = array(
'order' => 'ASC'
);
query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('content'); ?>
<?php endwhile; ?>
我想要的是创建两个链接,用户单击并更改此参数,从旧到新或从新到旧。
我曾想过使用jQuery来实现这一点,但我不知道如何根据用户单击的链接来更改PHP代码。
如果您将排序方向更改为一个参数,例如
<?php
$args = array(
'order' => (isset($_GET['dir']) ? $_GET['dir'] : 'ASC')
);
query_posts( $args );
?>
然后你可以创建一个链接,比如:
<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>