开发者

php & wordpress ASC / DESC using href AND sorting

I have been battling with trying to get this code working on my site. I would like to have the ability for the user to sort posts in ascending or descending order from clicking href link. This option开发者_JAVA技巧 should be remembered when the user then chooses to sort the post list by another option e.g. title, votes, date.

Here is what I have got far:

<?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(); ?>

The href for sorting works just fine however the ASC / DESC do not do anything the whole thing just stays DESC.


Way too complicated. Just store The user ASC/DESC preference in a Cookie (via JS, if you prefer a quick&dirty solution), use The Cookie Value for Setting Order when sorting.


Instead of using confusing letters, I can suggest 2 better solutions:

  1. Use a base-3 number (1021021210) to determine which fields are not sorted (0), ASC (1) or DESC (2).
  2. Use PHP's $_GET superglobal and create URLs such as example.com/index.php?fielda=asc&fieldb=desc. Then parse it to see what the user wanted to sort.

Solution 2 is preferred.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜