如果函数存在,并且满足数组,则回显函数?

时间:2011-04-28 作者:RodeoRamsey

我有如下功能:

<?php echo my_custom_function( array( \'category_url\'=>\'My Special Category\' ) ); ?>
它从“我的特殊类别”中提取数据并显示出来。我想在它周围放一个条件语句,它说,如果,并且只有当,那个类别有东西要显示,要显示它。有多个category\\u url,因此使用if语句将其包装,并且由于数组的原因,仅使用my\\u custom\\u函数不起作用,它会抛出错误:“缺少参数1”。

如果这个函数存在并且参数本身相等,那么如何使它只回显?PHP noob问题。;)谢谢

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

以下函数向您展示了一些“最佳实践”示例;)

它包括错误处理、检查emtpy数组值、使用默认值解析输入参数、关心是否要回显结果等。您甚至还为您的子主题设置了一个过滤器默认值。:)

函数中的函数。php文件:

function wpse15886_check_cat( $args = array( \'cat_url\', \'other_args_for_the_output\', \'echo\' => false ) )
{
    // Abort if no \'cat_url\' argument was given
    if ( ! isset( $args[\'cat_url\'] ) )
        $error = __( \'You have to specify a category slug.\' );

    // check if the category exists
    $given_cat = get_category_by_slug( $args[\'cat_url\'] );

    if ( empty( $given_cat ) )
        $error = sprintf( __( \'The category with the slug %1$s does not exist.\' ), $args[\'cat_url\'] );

    // check if we got posts for the given category
    $the_posts = get_posts( array( \'category\' => $args[\'cat_url\'] ) );

    if ( empty( $the_posts ) )
        $error = __( \'No posts were found for the given category slug.\' );

    // error handling - generate the output
    $error_msg = new WP_Error( __FUNCTION__, $error );
    if ( is_wp_error( $error_msg ) ) 
    {
        ?>
            <div id="error-<?php echo $message->get_error_code(); ?>" class="error-notice">
                <strong>
                    <?php echo $message->get_error_message(); ?>
                </strong>
            </div>
        <?php 
        // abort
        return;
    }

    // Set some defaults
    $defaults = array(
         \'key a\' => \'val a\'
        ,\'key a\' => \'val a\'
        ,\'key a\' => \'val a\'
    );
    // filter defaults
    $defaults = apply_filters( \'filter_check_cats\', $defaults ); 

    // merge defaults with input arguments
    $args = wp_parse_args( $args, $defaults );
    extract( $args, EXTR_SKIP );

    // >>>> build the final function output
    $output = $the_posts;
    $output = __( \'Do what ever you need to do here.\' );
    $output = sprintf( __( \'Manipulating some of the parsed %1$s for example.\' ), $args[\'other_args_for_the_output\'] );
    // <<<< end build output

    // just return the value for further modification
    if ( $args[\'echo\'] === false )
        return $output;

    return print $output;
} 
请注意,我尚未测试该函数,因此可能存在一些键入错误等问题。但错误会引导你。

用例:

$my_cats = array(
     \'cat_slug_a\' 
    ,\'cat_slug_b\' 
    ,\'cat_slug_c\'
);

if ( function_exists( \'wpse15886_check_cat\' ) )
{
    foreach ( $my_cats as $cat )
    {
        $my_cats_args = array(
             $cat
            ,\'other_args_for_the_output\' // this could also be another array set in here
            ,\'echo\' => true
        );

        // trigger the function
        wpse15886_check_cat( $my_cats_args );
    }
}

结束

相关推荐

当我试图停用任何插件时,它会给出错误“WARNING:CALL_USER_FUNC_ARRAY()[Function.Call-User-Func-ARRAY]”

当我尝试取消激活任何插件时,它会出错。如何解决这个问题?插件被取消激活,但单击按钮“Deactivate”时,我收到此错误。警告:call\\u user\\u func\\u array()[函数.call user func array]:第一个参数应该是有效的回调,“youtuber\\u uninstall”在/home/username/public\\u html/wp includes/plugin中给出。php在线395**