0

例如,我有以下代码:

<?php 

$options = array( 

    array(
        "type" => "section",
        "icon" => "acera-icon-preference",
        "title" => "General Settings",
        "id" => "general",
        "expanded" => "true"
    ),
array(
        "under_section" => "ajax_login",
        "type" => "checkbox",
        "name" => "Who's Online Options",
        "id" => array("tigu_whosonline_list", "tigu_whosonline_avatar"),
        "display_checkbox_id" => "tigu_show_whosonline",
        "img_desc" => "", 
        "options" => array("Who's Online List", "Who's Online Avatars"),
        "desc" => "",
        "default" => array("checked", "checked")

) );

?>

有更多数组,但我缩短了代码以节省空间。问题是:我怎样才能只包含以下代码

 array(
            "type" => "section",
            "icon" => "acera-icon-preference",
            "title" => "General Settings",
            "id" => "general",
            "expanded" => "true"
        ),

在有条件的情况下,如果有人认为像这样的代码(会出错):

if(function_exists('bp_is_active') ): 

  array(
                "type" => "section",
                "icon" => "acera-icon-preference",
                "title" => "General Settings",
                "id" => "general",
                "expanded" => "true"
            ),

endif;

函数 bp_is_active 它检查是否安装了 BuddyPress 插件。仅当安装了 BuddyPress 插件时,我才需要在我的 wp 管理面板上显示该代码。

谢谢!

4

2 回答 2

0

您可以使用?:三元条件运算符:

$options = array(
  function_exists('bp_is_active') // condition
    ? array(...stuff...)          // if true
    : array(),                    // if false
  ...more arrays...
);

如果条件为假,那将使用一个空数组。如果你什么都不想要(而不是array()),你需要更复杂的东西。

于 2013-04-14T07:42:50.913 回答
0

我认为你只需要分号(;),以下代码是有效的:

 if(function_exists('bp_is_active') ): 
     array(
            "type" => "section",
            "icon" => "acera-icon-preference",
            "title" => "General Settings",
            "id" => "general",
            "expanded" => "true"
          ); // <<--here

    endif;

或没有endif

     if(function_exists('bp_is_active') ){ 
     array(
            "type" => "section",
            "icon" => "acera-icon-preference",
            "title" => "General Settings",
            "id" => "general",
            "expanded" => "true"
          ); // <<--here
     }
于 2013-04-14T07:45:23.693 回答