使用下拉菜单重新排序WP_QUERY

时间:2013-03-01 作者:Christopher Camplin

我使用以下循环按类别显示帖子(&A);我想根据下拉选择器重新订购

if (!isset($qry))
    $qry = new WP_Query(array(\'category_name\'=>$GLOBALS[\'city\'],\'order\'=>$orderby));    

if ($qry->have_posts($orderby)) {
    $do_the_post = null;

    if (is_single($orderby)) {
        $do_the_post = true;
    } else {
        $qry->the_post($orderby);
        $do_the_post = false;
    }

    $user = get_userdata($post->post_author);
到目前为止,使用我找到的一些代码here

我有:

if( isset($_GET[\'o\']) && $_GET[\'o\'] != \'\')
    {
    $order = $_GET[\'o\'];
    switch($order)
    {
        case \'date-asc\': $orderby = \'order=ASC\';
            $msg = \'Date Ascending\';
            break;
        case \'date-desc\': $orderby = \'order=DESC\';
        $msg = \'Date Descending(default)\';
            break;
        case \'date-mod\': $orderby = \'orderby=modified\';
            $msg = \'Date Modified\';
            break;
        case \'title-asc\': $orderby = \'orderby=title&order=ASC\';
            $msg = \'Title A-Z\';
            break;
        case \'title-desc\': $orderby = \'orderby=title&order=DESC\';
            $msg = \'Title Z-A\';
            break;
        case \'comment\': $orderby = \'orderby=comment_count\';
            $msg = \'Comment Count\';
            break;
    }
    }
    else
    {
        $orderby = \'order=DESC\';
        $msg = \'Date Descending (default)\';
    }


            <div id="sorter-container">
            <select id="order-by">
                <option value="date-desc" <?php echo (!isset($order) || $order == \'\' || $order == \'date-desc\')? \'selected="selected"\':\'\'; ?>>Date Desc. (default)</option>
                <option value="date-asc" <?php echo ($order == \'date-asc\')? \'selected="selected"\':\'\'; ?>>Date Asc</option>
                <option value="date-mod" <?php echo ($order == \'date-mod\')? \'selected="selected"\':\'\'; ?>>Date Modified</option>
                <option value="title-desc" <?php echo ($order == \'title-desc\')? \'selected="selected"\':\'\'; ?>>Title Desc.</option>
                <option value="title-asc" <?php echo ($order == \'title-asc\')? \'selected="selected"\':\'\'; ?>>Title Asc.</option>
                <option value="comment" <?php echo ($order == \'comment\')? \'selected="selected"\':\'\'; ?>>Comments Count</option>
        </select>
        </div>
//JQUERY

    <script type="text/javascript">
        var orderby = jQuery(\'#order-by\');
        var str;
        orderby.change(function(){
        str = jQuery(this).val();
        window.location.href = "<?php echo home_url(); ?>/gallery/?o="+str;
    });
    </script>
正在调用$orderby,因为该命令不起作用-我的PHP最好是初级的,但有人能告诉我这里出了什么问题吗?

谢谢

1 个回复
最合适的回答,由SO网友:Jesse 整理而成

我想问题是

$qry = new WP_Query(array(\'category_name\'=>$GLOBALS[\'city\'],\'order\'=>$orderby)); 
就这么说吧$orderbyorderby=title&order=ASC

那么WP\\u Query的order参数将如下所示:

\'order\'=>\'orderby=title&order=ASC\'
但是,order参数仅接受:ASC and DESC.

你应该这样做:

WP_Query(\'category_name=\'.$GLOBALS[\'city\'].\'&\'.$orderby);

结束

相关推荐

包含一个PHP页面的URL前缀

是否可以将一个URL前缀设置为每次包含一个PHP页面?只有Wordpress标题和此PHP页面。我需要包括一些自定义应用程序,但必须包括wordpress支持的web设计。例如-http://my.web.com/customapp/ 是前缀,并且http://my.web.com/customapp/ http://my.web.com/customapp/some/function1 http://my.web.com/customapp/some/function2/x/y