繁体   English   中英

PHP和WordPress的ASC / DESC使用href和排序

[英]php & wordpress ASC / DESC using href AND sorting

我一直在努力使此代码在我的网站上正常工作。 我希望用户能够通过单击href链接按升序或降序对帖子进行排序。 用户随后选择按其他选项(例如标题,投票,日期)对帖子列表进行排序时,应记住该选项。

这是我所取得的成就:

<?php $sort= $_GET['sort'];
if($sort == "A")
{
$order= "gdsr_sort=thumbs";
}
if($sort == "B")
{
$order= "orderby=title";
}
if($sort == "C")
{
$order= "orderby=date";
}
?>

<?php $updown= $_GET['updown'];

if($updown == "Y")
{$ascend= "ASC";} //this orders in ascending
if($updown == "Z")
{$ascend= "DESC";} //this orders in descending
?>

<a href="?sort=A&updown=<?php echo $updown?>">Thumbs</a>
<a href="?sort=B&updown=<?php echo $updown?>">Author</a>
<a href="?sort=C&updown=<?php echo $updown?>">Date</a>

<?php $sort= isset($_GET['sort']) ? $_GET['sort'] : "B";

?>
<a href="?updown=Y&sort=<?php echo $sort?>">Ascending</a>
<a href="?updown=Z&sort=<?php echo $sort?>">Descending</a>

<?php query_posts($order.'&order=$.ascend.'); ?>

        <?php if ( have_posts() ) : ?>
            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

用于排序的href效果很好,但是ASC / DESC不会做任何事情,整个事情只会停留在DESC上。

除了使用混乱的字母,我还可以建议2个更好的解决方案:

  1. 使用以3为底的数字( 1021021210 )确定哪些字段未排序(0),ASC(1)或DESC(2)。
  2. 使用PHP的$_GET超全局变量并创建URL,例如example.com/index.php?fielda=asc&fieldb=desc 然后解析它以查看用户想要排序的内容。

溶液2是优选的。

方式太复杂了。 只需将用户ASC / DESC首选项存储在Cookie中(通过JS,如果您更喜欢快速和肮脏的解决方案),则在排序时使用Cookie值来设置订单。

暂无
暂无

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

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