Dynamic function name in php

后端 未结 6 1052
悲&欢浪女
悲&欢浪女 2021-01-04 16:16

I would like to simplify the creation of \"Custom Post Types\" in WordPress as it is tedious to go through the same script and change all the custom post type name instances

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 16:58

    This post is old, but PHP 7 may solve this question.

    Try:

    $prefix = 'news';
    
    $functionName = $prefix . "_custom_type_init";
    
    ${$functionName} = function() use ($prefix) {
    
        register_post_type($prefix, array(
            'labels' => array(
                'name' => $prefix,
                'singular_label' => $prefix,
                'add_new' => 'Add'
            )
        );
    
        register_taxonomy_for_object_type( 'category', $prefix );
    };
    
    add_action('init', '$' . $functionName);
    

    I think it should work for WordPress.

提交回复
热议问题