如何将变量用作函数名?

时间:2013-04-15 作者:Downloadtaky

我正在尝试循环一个函数,该函数将允许用户创建一些自定义帖子,但我对函数名有一个问题。以下是一个示例:

我该如何写:

function register_cpt_1_mdframework_price(){
…就像这样:

function $myvar(){
我就是这样解决的,但这既不好也不聪明——还有更好的主意吗?

<?php
    $numcpost = of_get_option(\'how_many_custom_posts\');
    do {
        switch ($numcpost){
            case 1:
                require_once MDCP_DIR . \'cpost/cpostloop1.php\';
                break;
            case 2:
                require_once MDCP_DIR . \'cpost/cpostloop2.php\';
                break;
            case 3:
                require_once MDCP_DIR . \'cpost/cpostloop3.php\';
                break;
            case 4:
                require_once MDCP_DIR . \'cpost/cpostloop4.php\';
                break;
            case 5:
                require_once MDCP_DIR . \'cpost/cpostloop5.php\';
                break;
        };
    --$numcpost;
}while ($numcpost > 0);
文件:cpostloop1。php
<?php
    $numcpost = 1;
    add_action( \'init\', \'register_cpt_\'.$numcpost.\'_mdframework_price\', $numcpost );
    $singcpost = of_get_option(\'custom_posts_name_s_n\'.$numcpost.\'\');
    $plurcpost = of_get_option(\'custom_posts_name_p_n\'.$numcpost.\'\');
    $desccpost = of_get_option(\'custom_posts_name_d_n\'.$numcpost.\'\');
    $imgcpost = of_get_option(\'custom_posts_name_i_n\'.$numcpost.\'\');
    function register_cpt_1_mdframework_price(){
        $labels = array( 
            \'name\' => _x( $plurcpost, \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'singular_name\' => _x( $singcpost, \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'add_new\' => _x( \'Add New\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'add_new_item\' => _x( \'Add New\'.$singcpost.\'\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'edit_item\' => _x( \'Edit\'.$singcpost.\'\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'new_item\' => _x( \'New\'.$singcpost.\'\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'view_item\' => _x( \'View\'.$singcpost.\'\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'search_items\' => _x( \'Search\'.$plurcpost.\'\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'not_found\' => _x( \'No \'.$plurcpost.\' found\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'not_found_in_trash\' => _x( \'No \'.$plurcpost.\' found in Trash\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'parent_item_colon\' => _x( \'Parent \'.$singcpost.\':\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'menu_name\' => _x( $singcpost, \'mdframework_custom_created_post\'.$numcpost.\'\' ),
        );
        $args = array( 
            \'labels\' => $labels,
            \'hierarchical\' => true,
            \'description\' => $desccpost,
            \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'page-attributes\' ),
            \'taxonomies\' => array( \'category\', \'post_tag\', \'page-category\', \'high\', \'middle\', \'low\', \'special\', \'last_minute\' ),
            \'public\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'menu_position\' => 5,
            \'menu_icon\' => $imgcpost,
            \'show_in_nav_menus\' => true,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => false,
            \'has_archive\' => true,
            \'query_var\' => true,
            \'can_export\' => true,
            \'rewrite\' => true,
            \'capability_type\' => \'post\'
        );
    register_post_type( \'mdframework_custom_created_post_\'.$numcpost.\'\', $args );
    }

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

<?php
$numcpost = of_get_option(\'how_many_custom_posts\');

do {

    add_action( \'init\', \'register_cpt_mdframework_price\', $numcpost );
    --$numcpost;

} while ( $numcpost > 0 );

function register_cpt_mdframework_price( $numcpost ) {

    $singcpost = of_get_option(\'custom_posts_name_s_n\'.$numcpost.\'\');
    $plurcpost = of_get_option(\'custom_posts_name_p_n\'.$numcpost.\'\');
    $desccpost = of_get_option(\'custom_posts_name_d_n\'.$numcpost.\'\');
    $imgcpost  = of_get_option(\'custom_posts_name_i_n\'.$numcpost.\'\');

    $labels = array();
        // labels for $numcpost = 1
    $labels[1] = array(/*typo was here*/
            \'name\' => _x( $plurcpost, \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'singular_name\' => _x( $singcpost, \'mdframework_custom_created_post\'.$numcpost.\'\' ),
            \'add_new\' => _x( \'Add New\', \'mdframework_custom_created_post\'.$numcpost.\'\' ),
    );

        // labels for $numcpost = 2
    $labels[2] = array(
        // ...
    );

    $args = array();

        // args for $numcpost = 1
    $args[1] = array(
            \'labels\' => $labels[1],
            \'hierarchical\' => true,
            \'description\' => $desccpost,    
    );

        // args for $numbercpost = 2
    $args[2] = array(
            \'labels\' => $labels[2]
        // ...
    );

    register_post_type( \'mdframework_custom_created_post_\'.$numcpost, $args[$numcpost] );
}
使用多维数组。很简单,不是吗?

结束

相关推荐

生产环境中的WordPress PHP错误处理和报告

我试图找到一种解决方案,解决如何在PHP(Wordpress)生产环境中显示和记录错误的问题。Is it possible to do the following (alternative suggestions welcome):捕获关键错误并立即发送电子邮件通知(this looks like a promising solution, but haven\'t tested in my production env yet).</不要在屏幕上向访问者显示PHP错误。而是显示自定义生成的错误页。