将变量传递给REGISTER_TASTIONY()

时间:2015-01-28 作者:user27251

我正在尝试创建多个分类法。如下所示。。。。。

$taxonomies = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($taxonomies as $taxonomy) {
  //stuff that defines the taxonomy
register_taxonomy( $taxonomy->singular_name, \'book\', $tagArgs );
}
似乎WP不喜欢用$taxonomy->singular_name. Xdebug向我提供了以下错误。

Notice: Trying to get property of non-object in /home/wpplugin/public_html/wp-includes/link-template.php on line 898
我试过几种不同的方法。将对象转换为数组并将其输入register\\u taxonomy(),我将$taxonomy->singular\\u name转换为字符串和其他几个随机猜测对象,得到了相同的结果。唯一有效的方法是用引号代替$taxonomy->singular\\u name,这不符合我的需要。

有人对此有什么想法吗??ThanksGreg感谢

此处为完整代码。。。。。

add_action( \'init\', \'create_book_taxonomies\', 0 );function create_book_taxonomies() {
global $wpdb;
$table_name = $wpdb->prefix . \'wlc_custom_taxonomy\';
$taxonomies = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($taxonomies as $taxonomy) {
    $enterMeOnce = $taxonomy->singular_name;
    $book = $taxonomy->singular_name;

    if($taxonomy->singular_name != \'singular-name\'){ //exclude the default table row
        $resLabels = array(
                \'name\'               => _x( $enterMeOnce , \'Post Type General Name\' ),
                \'singular_name\'      => _x( $enterMeOnce , \'Post Type Singular Name\' ),
                \'menu_name\'          => __( $enterMeOnce  ),
                \'name_admin_bar\'     => __( $enterMeOnce  ),
                \'all_items\'          => __( $enterMeOnce  ),
                \'add_new_item\'       => __( \'Add New \' . $enterMeOnce  ),
                \'edit_item\'          => __( \'Edit \' . $enterMeOnce  ),
                \'new_item\'           => __( \'New \' . $enterMeOnce  ),
                \'view_item\'          => __( \'View \' . $enterMeOnce  ),
                \'search_item\'        => __( \'Search \' . $enterMeOnce  ),
                \'not_found\'          => __( \'No \'.$enterMeOnce .\' found\' ),
                \'not_found_in_trash\' => __( \'No \'.$enterMeOnce .\' found in trash\' ),
                \'description\'         => __( $enterMeOnce .\' are special posts that are showcased on the \'.$enterMeOnce .\' page.\' ),
            );
        $resArgs = array(
                \'labels\' => $resLabels,

                \'public\'              => true,
                \'exclude_from_search\' => false,
                \'publcly_queryable\'   => true,
                \'show_ui\'             => true,
                \'show_in_menu\'        => true,
                \'show_in_nav_menus\'   => true,
                \'show_in_admin_bar\'   => true,
                \'menu_position\'       => 5,
                \'menu_icon\'           => \'dashicons-lightbulb\',
                \'capability_type\'     => \'post\',
                \'meta_box_cb\'         => true,
                \'show_admin_column\'   => true,
                \'hierarchical\'        => true,
                \'has_archive\'         => true,
                //\'rewrite\'           => true,
                /*\'capabilities\'        => array(
                    \'manage_terms\' => \'manage_resource\',
                    \'edit_terms\'   => \'manage_categories\',
                    \'delete_terms\' => \'manage_categories\',
                    \'assign_terms\' => \'edit_posts\'
                ),*/
                \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'post_format\' )
            );

    register_post_type( $book , $resArgs );

        $labels = array(
                \'name\'                       => _x( $taxonomy->name . \' Categories\', \'Taxonomy General Name\', \'text_domain\' ),
                \'singular_name\'              => _x( $taxonomy->name . \' Category\', \'Taxonomy Singular Name\', \'text_domain\' ),
                \'menu_name\'                  => __( $taxonomy->name . \' Categories\', \'text_domain\' ),
                \'all_items\'                  => __( \'All Categories\', \'text_domain\' ),
                \'parent_item\'                => __( \'Parent Category\', \'text_domain\' ),
                \'parent_item_colon\'          => __( \'Parent Category:\', \'text_domain\' ),
                \'new_item_name\'              => __( \'New Resource Category\', \'text_domain\' ),
                \'add_new_item\'               => __( \'Add New Resource Category\', \'text_domain\' ),
                \'edit_item\'                  => __( \'Edit Category\', \'text_domain\' ),
                \'update_item\'                => __( \'Update Category\', \'text_domain\' ),
                \'separate_items_with_commas\' => __( \'Separate resource cat names with commas\', \'text_domain\' ),
                \'search_items\'               => __( \'Search Resource Categories\', \'text_domain\' ),
                \'add_or_remove_items\'        => __( \'Add or remove resource categories\', \'text_domain\' ),
                \'choose_from_most_used\'      => __( \'Choose from the most used Categories\', \'text_domain\' ),
                \'not_found\'                  => __( \'Resource Category Not Found\', \'text_domain\' ),
                \'slug\'                       => __( $taxonomy->name . \'category\' )
            );

            $args = array(
                \'labels\'                     => $labels,
                \'hierarchical\'               => true,
                \'public\'                     => true,
                \'has_archive\'                => true,
                \'show_ui\'                    => true,
                \'show_admin_column\'          => true,
                \'show_in_nav_menus\'          => true,
                \'show_tagcloud\'              => false,
                \'rewrite\'                    => true,
            );


            register_taxonomy( $taxonomy->singular_name, array( $book ), $args );

            $tagLabels = array(
                \'name\'                       => _x( \'Writers\', \'taxonomy general name\' ),
                \'singular_name\'              => _x( \'Writer\', \'taxonomy singular name\' ),
                \'search_items\'               => __( \'Search Writers\' ),
                \'popular_items\'              => __( \'Popular Writers\' ),
                \'all_items\'                  => __( \'All Writers\' ),
                \'parent_item\'                => null,
                \'parent_item_colon\'          => null,
                \'edit_item\'                  => __( \'Edit Writer\' ),
                \'update_item\'                => __( \'Update Writer\' ),
                \'add_new_item\'               => __( \'Add New Writer\' ),
                \'new_item_name\'              => __( \'New Writer Name\' ),
                \'separate_items_with_commas\' => __( \'Separate writers with commas\' ),
                \'add_or_remove_items\'        => __( \'Add or remove writers\' ),
                \'choose_from_most_used\'      => __( \'Choose from the most used writers\' ),
                \'not_found\'                  => __( \'No writers found.\' ),
                \'menu_name\'                  => __( $taxonomy->name . \' Tags\' ),
            );

            $tagArgs = array(
                \'hierarchical\'          => false,
                \'labels\'                => $tagLabels,
                \'show_ui\'               => true,
                \'show_admin_column\'     => true,
                \'update_count_callback\' => \'_update_post_term_count\',
                \'query_var\'             => true,
                \'rewrite\'               => array( \'slug\' => \'writer\' ),
            );

            register_taxonomy( $taxonomy->singular_name, $book, $tagArgs );


    }
}
}

1 个回复
SO网友:user27251

事实证明,这里的问题是foreach声明。我将循环更改为

foreach ($taxonomies as $taxonomy) {

for ($counter=0; $counter < sizeof($taxonomy); $counter++) { 
以及所有相关人员,所以。。。。

register_taxonomy( $taxonomy->singular_name, \'book\', $tagArgs );
成为

register_taxonomy( $taxonomy[$counter]->name, $book, $args );
一切都很顺利。

我怀疑这是主机内存的问题,但我不确定是否有人知道这是为什么,我想听听。但现在这个问题已经解决了。

结束

相关推荐