$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Can one create multiple Custom Post Types with a for loop?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Can one create multiple Custom Post Types with a for loop?

matteradmin10PV0评论

I am trying to use a for loop to create multiple custom post types in a plugin. The plugin options page allows users to set the number of custom post types along with the singular and plural names for the custom post types.

When I run the code below wordpress is only registering the last custom post type. Here is the code I'm using:

<?php 
if($options_foo['num_post_types'] > 0 ) {
for($i =1; $i <= $options_foo['num_post_types']; $i++) {
    global $options_foo;
    $singular = $options_foo['post_type_names'][$i]['singular'];
    $plural = $options_foo['post_type_names'][$i]['plural'];
    add_action('init', function(){
        global $singular;
        global $plural;
        $labels = array(
            'name'                  => $plural,
            'singular_name'         => $singular,
            'add_new'               => "New $singular",
            'add_new_item'          => "New $singular",
            'edit_item'             => "Edit $singular",
            'new_item'              => "New $singular",
            'view_item'             => "View $singular",
            'view_items'            => "View $plural",
            'search_items'          => "Search $plural",
            "not_found"             => "No $plural Found",
            "not_found_in_trash"    => "No $plural Found in Trash",
            'all_items'             => "All $plural",
            'attributes'            => "$singular Attributes",
            'insert_into_item'      => "Insert to $singular",
            'uploaded_to_this_item' => "Uploaded to this $singular"
        );
        $supports = array('title', 'thumbnail');
        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'exclude_from_search'   => true,
            'publicly_queryable'    => true,
            'show_in_nav_menus'     => false,
            'show_in_admin_bar'     => false,
            'menu_position'         => 5,
            'menu_icon'             => 'dashicons-admin-home',
            'supports'              => $supports,
            'can_export'            => 'true'
        );
        register_post_type($singular, $args);
    });
}//end for
}//end if

I am trying to use a for loop to create multiple custom post types in a plugin. The plugin options page allows users to set the number of custom post types along with the singular and plural names for the custom post types.

When I run the code below wordpress is only registering the last custom post type. Here is the code I'm using:

<?php 
if($options_foo['num_post_types'] > 0 ) {
for($i =1; $i <= $options_foo['num_post_types']; $i++) {
    global $options_foo;
    $singular = $options_foo['post_type_names'][$i]['singular'];
    $plural = $options_foo['post_type_names'][$i]['plural'];
    add_action('init', function(){
        global $singular;
        global $plural;
        $labels = array(
            'name'                  => $plural,
            'singular_name'         => $singular,
            'add_new'               => "New $singular",
            'add_new_item'          => "New $singular",
            'edit_item'             => "Edit $singular",
            'new_item'              => "New $singular",
            'view_item'             => "View $singular",
            'view_items'            => "View $plural",
            'search_items'          => "Search $plural",
            "not_found"             => "No $plural Found",
            "not_found_in_trash"    => "No $plural Found in Trash",
            'all_items'             => "All $plural",
            'attributes'            => "$singular Attributes",
            'insert_into_item'      => "Insert to $singular",
            'uploaded_to_this_item' => "Uploaded to this $singular"
        );
        $supports = array('title', 'thumbnail');
        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'exclude_from_search'   => true,
            'publicly_queryable'    => true,
            'show_in_nav_menus'     => false,
            'show_in_admin_bar'     => false,
            'menu_position'         => 5,
            'menu_icon'             => 'dashicons-admin-home',
            'supports'              => $supports,
            'can_export'            => 'true'
        );
        register_post_type($singular, $args);
    });
}//end for
}//end if
Share Improve this question asked Feb 27, 2019 at 19:17 Edward SeibertEdward Seibert 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have a problem with scopes in your code and you misuse actions, I guess.

Let me explain... Let's take a look at this part of code:

for($i =1; $i <= $options_foo['num_post_types']; $i++) {
    global $options_foo;
    $singular = $options_foo['post_type_names'][$i]['singular'];
    $plural = $options_foo['post_type_names'][$i]['plural'];
    add_action('init', function(){
        global $singular;

What it does, is:

  1. You loop through all options and for every one of them: a) You set some global variables b) You register new action to init hook.

So some time later, the hook init causes your actions to be called one by one. But all of these actions will use the same value of the global variables (because they were set long before...)

Here's how it should be done:

add_action( 'init', function () {
    global $options_foo;

    if ( $options_foo['num_post_types'] > 0 ) {
        for ( $i=1; $i <= $options_foo['num_post_types']; $i++) {
            $singular = $options_foo['post_type_names'][$i]['singular'];
            $plural = $options_foo['post_type_names'][$i]['plural'];

            $labels = array(
                'name'                  => $plural,
                'singular_name'         => $singular,
                'add_new'               => "New $singular",
                'add_new_item'          => "New $singular",
                'edit_item'             => "Edit $singular",
                'new_item'              => "New $singular",
                'view_item'             => "View $singular",
                'view_items'            => "View $plural",
                'search_items'          => "Search $plural",
                "not_found"             => "No $plural Found",
                "not_found_in_trash"    => "No $plural Found in Trash",
                'all_items'             => "All $plural",
                'attributes'            => "$singular Attributes",
                'insert_into_item'      => "Insert to $singular",
                'uploaded_to_this_item' => "Uploaded to this $singular"
            );
            $supports = array('title', 'thumbnail');
            $args = array(
                'labels'                => $labels,
                'public'                => true,
                'exclude_from_search'   => true,
                'publicly_queryable'    => true,
                'show_in_nav_menus'     => false,
                'show_in_admin_bar'     => false,
                'menu_position'         => 5,
                'menu_icon'             => 'dashicons-admin-home',
                'supports'              => $supports,
                'can_export'            => 'true'
            );
            register_post_type($singular, $args);
        }
    }
});
Post a comment

comment list (0)

  1. No comments so far