1

我不知道在 php 的数组中不能使用 if else 语句。我搜索了stackoverflow,发现速记版本实际上应该可以正常工作。

我试过了,但仍然有错误,页面没有加载,我的代码:

query_posts (array(

                                ($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "")

                                'order' => 'DESC',
                                'orderby' => $prefooter_order_logic,

                                'posts_per_page' => '10',
                                'post_type' => 'post',
                                'category_name' => $prefooter_category_select

                            ));

这不会给我错误:

但它不起作用......

($prefooter_order_logic == 'xviews' ? "

                                    'v_sortby' => 'views',
                                    'v_orderby' => 'desc',

                                    " : "

                                    'order' => 'DESC',
                                    'orderby' => $prefooter_order_logic,

                                    "),

我决定停止玩耍并做最简单的方法:

顺便说一句,如果这是最好的方法,有什么想法吗?或不?

                if ($prefooter_order_logic == 'xviews') {

                    query_posts (array(
                        'v_sortby' => 'views',
                        'v_orderby' => 'desc',
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                } else {

                    query_posts (array(
                        'order' => 'DESC',
                        'orderby' => $prefooter_order_logic,
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                }
4

4 回答 4

2

这里的问题首先是)?,结束 后没有

$prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "")

应该

($prefooter_order_logic == 'xviews' )? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""),
于 2012-10-12T06:17:11.997 回答
2

这将产生syntax error, unexpected T_CONSTANT_ENCAPSED_STRING,因为您在此行末尾缺少逗号:

($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""), 
                                                                                           ^ here

编辑:

只需在创建数组后根据您的条件修改数组:

$arr = array(

    'posts_per_page' => '10',
    'post_type' => 'post',
    'category_name' => $prefooter_category_select

);

if($prefooter_order_logic == 'xviews')
{
    $arr['v_sortby'] = 'views';
    $arr['v_orderby'] = 'desc';
}
else
{
    $arr['order'] = 'DESC';
    $arr['orderby'] = $prefooter_order_logic;
}

query_posts($arr);

或者,使用array_merge()

$arr = array(

    'posts_per_page' => '10',
    'post_type' => 'post',
    'category_name' => $prefooter_category_select

);

$arr = $prefooter_order_logic == 'xviews' ? array_merge($arr, array('v_sortby' => 'views', 'v_orderby' => 'desc')) : array_merge($arr, array('order' => 'desc', 'orderby' => $prefooter_order_logic));

query_posts($arr);
于 2012-10-12T06:19:55.603 回答
1

您应该指定您有哪些错误,并且很难说出您正在尝试做什么。

但是,您在三元运算符的行尾缺少逗号:

'desc'," : "")

后面加逗号。

于 2012-10-12T06:19:02.433 回答
0

我猜你不能像这样评估 PHP 代码:

"'v_sortby' => 'views', 'v_orderby' => 'desc',"

它应该看起来像 PHP 解释器的一段文本,而不是你的数组的一部分,但我不确定。

于 2012-10-12T06:31:35.193 回答