【发布时间】:2019-12-15 06:02:36
【问题描述】:
我正在编写如下所示的 php 代码:
$special_reports = new \WP_Query([
'post_type' => 'cpac-special-report',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => $data->{"toggle_status"} == 3 ? 2 : 4,
'posts_per_page' => $data->{"toggle_multi_status"} == 1 ? 3 : 4,
'posts_per_page' => $data->{"toggle_multi_status"} == 2 ? 1 : 4,
'posts_per_page' => $data->{"toggle_multi_status"} == 3 ? 2 : 4,
'posts_per_page' => $data->{"toggle_multi_status"} == 4 ? 3 : 4
]);
我想最小化上面的 php 代码并使用多个三元运算符。这是 我尝试过,但我没有得到想要的 o/p。
$special_reports = new \WP_Query([
'post_type' => 'cpac-special-report',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => $data->{"toggle_multi_status"} == 1 ? 3 : $data->{"toggle_multi_status"} == 2 ? 1 : $data->{"toggle_multi_status"} == 3 ? 2 : $data->{"toggle_multi_status"} == 4 ? 1:4
]);
问题陈述:
我想知道我应该在上面尝试过的 php 代码中进行哪些更改,以便何时,
$data->{"toggle_multi_status"} == 1 then it should print 3
$data->{"toggle_multi_status"} == 2 then it should print 1
$data->{"toggle_multi_status"} == 3 then it should print 2
$data->{"toggle_multi_status"} == 4 then it should print 3
【问题讨论】:
-
$data->{"toggle_status"} 和sandbox.onlinephpfunctions.com/code/…没有关系
标签: php wordpress ternary-operator