php&wordpress ASC/DESC使用href和排序
问题描述:
我一直在努力争取在我的网站上运行这段代码。 我希望用户能够通过单击href链接以升序或降序对帖子进行排序。 当用户选择通过其他选项对帖子列表进行排序时,应该记住此选项,例如,标题,投票,日期。php&wordpress ASC/DESC使用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(); ?>
排序的作品就好了,不过ASC/DESC没有做任何事情,整个事情只是停留DESC在href。
答
代替使用混乱字母,我可以建议2个更好的解决方案:
- 使用碱-3-号码(
1021021210
),以确定哪些字段不排序(0),ASC(1)或DESC( 2)。 - 使用PHP的
$_GET
超全球化并创建网址,例如example.com/index.php?fielda=asc&fieldb=desc
。 然后解析它以查看用户想要分类的内容。
解决方案2是优选的。
答
太复杂了。只需将用户ASC/DESC首选项存储在Cookie中(通过JS,如果您更喜欢快速&肮脏的解决方案),请在排序时使用Cookie值来设置订单。
+0
你有没有我如何做到这一点的例子请wildrot? 感谢转载 simon –
我不知道使用base-3号码会涉及什么?! http://www.something.com/?updown=1&sort=A 我使用$ _GET为你的意思是有URL中ASC和DESC的网址是什么? 感谢您的帮助 –
表示,其中0是没有排序字段的地方1,2,3,等国家,1是ASC和2 DESC,可以解析字符串你和确定哪些字段进行排序如何,第二种解决方案虽然更好。 –
我承认我在这里有点出乎我的意料, 你能帮我一个例子吗? Simon –