在数组中嵌入IF语句?

时间:2012-10-05 08:40:07

标签: php arrays wordpress

我有一个数组如下

<?php
$args = array(
"post_author_url" => "no",
"thumbnail_custom_field" => "image",
"post_include" => get_user_meta($userID, 'member_owner', true),
"layout_mode" => "multi_column","layout_num_cols" => "3");
special_recent_posts($args);
?>

这很好用,但我想在其中一个项目上有一个条件

"thumbnail_custom_field" => "image",

"image"可能不存在,所以我需要退回以便显示OK。

例如:

<?php if ( get_post_meta($post->ID, 'image', true)) {
echo 'image'
} else { 
echo 'image-fallback'
?>

如何通过此IF语句在数组中添加image标记?我是否在数组之前将其作为字符串?或者我将IF语句直接放入数组中?寻找一些专家指导..谢谢

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

<?php
$args = array(
"post_author_url" => "no",
"thumbnail_custom_field" => ((get_post_meta($post->ID, 'image', true))?'image':'image-fallback'),
"post_include" => get_user_meta($userID, 'member_owner', true),
"layout_mode" => "multi_column","layout_num_cols" => "3");
special_recent_posts($args);
?>

表达式(expr1)? (expr2):如果expr1的计算结果为TRUE,则(expr3)求值为expr2;如果expr1求值为FALSE,则求expr3。查找“三元运算符”。