dtvjl64442 2014-03-06 16:53
浏览 22
已采纳

将多个数组添加到args数组

SOLVED (Credits will be added in 10 min.)

I have a custom post type script in my functions.php (Wordpress) of which I do not understand its meaning.

$query_args = array(
'posts_per_page' => 5,
'post_type'      => $is_some_page ? CPT_ONE : CPT_TWO,
'post_status'    => 'publish',
'paged'          => 1
);

The script is loading perfect, no problems there.

Problem: I would like to add an additional post_type but I don't understand the : symbol... What is its meaning? And how to add more? This doesn't work:

'post_type' => $is_some_page ? CPT_ONE : CPT_TWO : CPT_THREE,

EDIT: Thanks for having a look and changing the labels for the question. Many thanks to everybody else for pointing me in the right direction with the operators.

  • 写回答

1条回答 默认 最新

  • doujiang6944 2014-03-06 16:55
    关注

    What you are looking at is the ternary if operator (a lot of languages have this, including both PHP and Javascript).

    So this:

    $someVar = $someBool ? $val1 : $ val2
    

    is equivalent to:

    if ($someBool)
    {
        $someVar = $val1;
    }
    else
    {
        $someVar = $val2;
    }
    

    So this:

    'post_type' => $is_some_page ? CPT_ONE : CPT_TWO : CPT_THREE
    

    doesn't make sense because $is_some_page can only evaluate as true or false. But it's not clear what your logic should be. Under what situation do you want post_type to be CPT_ONE, CPT_TWO or CPT_THREE?

    You could do something like this:

    'post_type' => $is_some_page ? CPT_ONE : ($some_other_condition ? CPT_TWO : CPT_THREE)
    

    Which is equivalent to an if / else if / else

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化