get_terms return errors

时间:2011-03-30 作者:Anjum

嗨,当我尝试get_terms(); 通过此代码在主题选项中

$catalogs_terms = get_terms( \'catalogs\' );
   $mycatalogs = array( -1 => \'Select a catalog\' );
   if ( $catalogs_terms ) {
      foreach ( $catalogs_terms as $catalog_term ) {
         $mycatalogs[$catalog_term->term_id] = $catalog_term->name;
      }
   }
返回空,但当我尝试print_r( $catalogs_terms ) 输出我收到错误

Array ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid Taxonomy ) ) [error_data] => Array ( ) )
我不明白我错在哪里?我的注册分类功能

    add_action( \'init\', \'my_taxonomies\', 0 );

function my_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\' => _x( \'Catalogs\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Catalog\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Catalogs\', \'mytextdomain\' ),
        \'all_items\' => __( \'All Catalogs\', \'mytextdomain\' ),
        \'parent_item\' => __( \'Parent Catalog\', \'mytextdomain\' ),
        \'parent_item_colon\' => __( \'Parent Catalog:\', \'mytextdomain\' ),
        \'edit_item\' => __( \'Edit Catalog\', \'mytextdomain\' ), 
        \'update_item\' => __( \'Update Catalog\', \'mytextdomain\' ),
        \'add_new_item\' => __( \'Add New Catalog\', \'mytextdomain\' ),
        \'new_item_name\' => __( \'New Catalog Name\', \'mytextdomain\' ),
        \'menu_name\' => __( \'Catalogs\', \'mytextdomain\' ),
    );  

    // register catalogs hierarchical (like categories)
    register_taxonomy( \'catalogs\',
        array( \'news\' ),
        array( \'hierarchical\' => true,
            \'labels\' => $labels,
            \'show_ui\' => true,
            \'public\' => true,
            \'query_var\' => true,
            \'rewrite\' => array( \'slug\' => \'catalogs\' )
        )
    );
}

2 个回复
最合适的回答,由SO网友:t31os 整理而成

正如我之前提到的,这是在分类法注册之前发生的术语获取的情况。

init操作发生在包含主题的函数文件之后,因此如果您直接在函数文件中查找术语,那么您是在它们实际注册之前进行的。

下面是wp-settings.php 它包括主题功能,并且init 行动

// Load the functions for the active theme, for both parent and child theme if applicable.
if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . \'/functions.php\' ) )
    include( STYLESHEETPATH . \'/functions.php\' );
if ( file_exists( TEMPLATEPATH . \'/functions.php\' ) )
    include( TEMPLATEPATH . \'/functions.php\' );

do_action( \'after_setup_theme\' );

// Load any template functions the theme supports.
require_if_theme_supports( \'post-thumbnails\', ABSPATH . WPINC . \'/post-thumbnail-template.php\' );

register_shutdown_function( \'shutdown_action_hook\' );

// Set up current user.
$wp->init();

/**
 * Most of WP is loaded at this stage, and the user is authenticated. WP continues
 * to load on the init hook that follows (e.g. widgets), and many plugins instantiate
 * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
 *
 * If you wish to plug an action once WP is loaded, use the wp_loaded hook below.
 */
do_action( \'init\' );
正如您所看到的init 操作在包含主题函数文件之后才会启动,因此任何术语检索都必须在init之后进行。我不能再进一步建议您了,因为您只向我展示了部分代码,所以我不太清楚您试图在其中使用术语function的上下文,但它肯定不能直接在函数文件中调用(在连接到特定操作/过滤器的回调之外,因为代码很快就会运行)。

希望以上内容足以说明您的问题:)

Additional note:
这个函数在全局语句中缺少一个var(如果启用了debug,您会看到PHP通知)。

function news_updated_messages( $messages ) {
    global $post;
应该是。。

function news_updated_messages( $messages ) {
    global $post, $post_ID;
。。因为该函数内的代码引用了该var,但该变量在函数内没有作用域,所以我上面建议的更改将修复该问题。

后续#1

创建插件或主题页面时,首先必须设置/注册该页面,通常是这样做的。。

add_action(\'admin_menu\', \'my_theme_menu\');

function my_theme_menu() {
    add_theme_page( \'Theme Settings\', \'Theme Settings\', \'manage_options\', \'my-unique-identifier\', \'my_theme_settings\' );
}
function my_theme_settings() {
    
    // Code to display/handle theme options would be here
    // You get_terms() call should work inside this function just fine

}
如果主题页面的创建方式不同,那么我真的无能为力,因为他们倾向于使用与常规WordPress主题完全不同的框架。

SO网友:Exit

如果你不想让Wordpress胡说八道,忽略数据库中明显存在的分类法,不管它们是否注册,你可以随时使用这样的替换函数来获取分类法。

function custom_get_terms($taxonomy) {
 global $wpdb;

 $out = array();

 $a = $wpdb->get_results($wpdb->prepare("SELECT t.name,t.slug,t.term_group,x.term_taxonomy_id,x.term_id,x.taxonomy,x.description,x.parent,x.count FROM {$wpdb->prefix}term_taxonomy x LEFT JOIN {$wpdb->prefix}terms t ON (t.term_id = x.term_id) WHERE x.taxonomy=%s;",$taxonomy));

 foreach ($a as $b) {
  $obj = new stdClass();
  $obj->term_id = $b->term_id;
  $obj->name = $b->name;
  $obj->slug = $b->slug;
  $obj->term_group = $b->term_group;
  $obj->term_taxonomy_id = $b->term_taxonomy_id;
  $obj->taxonomy = $b->taxonomy;
  $obj->description = $b->description;
  $obj->parent = $b->parent;
  $obj->count = $b->count;
  $out[] = $obj;
 }

 return $out;
}

结束

相关推荐

Sitewide category terms

我希望通过多站点博客网络实现标准类别术语。目标是:在一个博客(或根博客)上创建一个类别,并在所有博客上创建该类别。每个博客都有自己的类别URL,例如,将测试名为“苹果”的类别。实例com/类别/苹果和其他测试。实例com/categories/apples-只在相应的博客上列出这些帖子我在代码中看到了global\\u术语的概念-我不确定这是否相关,因为我找不到很多关于这方面的文档,也不知道它是否在3.0中被弃用。x、 如果这就是我要找的,有人能举个例子吗?谢谢丹