可以使用for循环创建多个自定义Post类型吗?

时间:2019-02-27 作者:Edward Seibert

我正在尝试使用for循环在插件中创建多个自定义帖子类型。插件选项页面允许用户设置自定义帖子类型的数量以及自定义帖子类型的单数和复数名称。

当我运行下面的代码时,wordpress只注册最后一个自定义帖子类型。下面是我使用的代码:

<?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

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

我想,您的代码中的作用域有问题,并且您误用了操作。

让我解释一下。。。让我们看看这部分代码:

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;
它的作用是:

循环遍历所有选项,并针对其中的每一个选项:a)设置一些全局变量b)注册新操作init 挂钩init 使您的操作被逐一调用。但所有这些操作都将使用相同的全局变量值(因为它们早在…之前就设置好了)

下面是应该如何做:

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);
        }
    }
});

相关推荐

PHP Error in Wordpress Theme

所以我对wordpress的AWS多站点托管有一些问题。每隔几周,我的整个网站实例就会下降。第一次发生时,我收到了500个错误,所以我只是重新启动了实例&;一切都很好。但第二次没有出现500个错误,虽然重新启动工作正常,但理想情况下,我希望完全解决这个问题,所以我深入挖掘了一些&;查看我的php错误日志;找到此项:“PHP警告:my\\u menu\\u walker::start\\u el(&;$output,$item,$depth,$args)的声明应与第61行{filepath