背景
私はクラス内にカスタムの投稿タイプとカスタム分類を登録します。 WPの管理者の中で、私は両方の投稿の種類を見ると、私は分類を参照してください。WordPress:get_terms()がカスタム分類を参照できないのはなぜですか?クラスの中に登録されていますか?
簡体コード:
class Custom_Post_Type {
function __construct($init_data) {
if (is_admin()) {
add_action('init', array($this, 'create_ctp'));
add_action('admin_head', array($this, 'create_ctp_icons'));
add_action('save_post', array($this, 'save_ctp_custom_metadata'), 1, 2);
}
}
function create_ctp_taxonomy() {
register_taxonomy(
$post_type.'_type',
$post_type,
array(
'labels' => array(
'name' => $taxonomy_label,
'add_new_item' => 'Add New '.$taxonomy_label
),
'public' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true
)
);
register_post_type($post_type_slug,
array(
'labels' => array(),
'public' => true,
'has_archive' => false,
'supports' => $this->supports,
'register_meta_box_cb' => array($this, 'create_ctp_custom_metaboxes'),
'taxonomies' => array($taxonomy_slug),
)
);
}
}
繰り返しますが、これは管理エリア内で動作します。私は投稿を追加することができ、分類法がわかり、用語を追加することができます。
問題
フロントエンドには、get_taxonomies()
は、新しいカスタム分類を見ていない、とget_terms()
は、その中に用語が表示されません。
私はregister_taxonomy
のいくつかの例を試しました。クラスの外で使ったとき、フロントエンドに表示されます。しかし、私が内部のcreate_ctp_taxonomy関数にサンプルを移したところ、それらは突然get_taxonomies
に見えなくなりました。
これはなぜ起こりますか?
編集
私は別のもので遊んでてきたと私はここでの問題はinit
アクションのタイミングだと思います。アクションを追加するのではなく、__construct
関数からsetup関数の方向を呼び出すと、処理が開始されます。例:
class Custom_Post_Type {
function __construct($init_data) {
if (is_admin()) {
//add_action('init', array($this, 'create_ctp'));
add_action('admin_head', array($this, 'create_ctp_icons'));
add_action('save_post', array($this, 'save_ctp_custom_metadata'), 1, 2);
}
$this->create_cpt();
}
}
これを実行すると、私はinit
を使用してスキップします。しかし、これは標準的な慣習に違反しているようです。このように誰かが知っている欠点はありますか?
上記のコードがありませんが、$ post_typeは$ this-> post_typeの関数probで定義されていませんか? – David
例の簡略化されたコードです。実際のコードでは '$ this-> slug.'と書かれています。 –
そうではないので、クラスオブジェクトはどこに作成しますか? – David