Dynamic function name in php

后端 未结 6 1069
悲&欢浪女
悲&欢浪女 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 17:09

    I'm not sure any of the above actually answer the question about dynamically creating custom post types. This works for me though:

    $languages = ("English", "Spanish", "French");
    
    foreach($languages as $language):
    
        $example = function () use ($language) {
    
            $labels = array(
                    'name' => __( $language . ' Posts' ),
                    'singular_name' => __( $language . ' Post' )
            );
    
            $args = array(
       
                'labels'              => $labels,
                'hierarchical'        => false,
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                "rewrite" => array( "slug" => $language . "_posts", "with_front" => true ),
                'capability_type'     => 'page',
             );
            register_post_type( $language . "_posts", $args );  
    
        };
        add_action( 'init', $example, 0 );
    
    endforeach;
    

提交回复
热议问题