这是对this post, 概述在函数中无法使用get\\u terms函数的原因。php(实际上是通过ajax/admin\\u ajax.php调用)。
我可以在任何帖子、任何页面上获取术语(自定义税),除了在我的ajax函数中。在转储get\\u terms的值时,我收到了可怕的“分类法不存在”错误。
问题是,在调用函数查找术语后,分类法被注册。
问题是,如何尽早注册税务以便使用此功能?注册自定义帖子类型和分类的代码是函数中的第一个。php(通过外部php提供,以保持functions.php干净)
/************************************************
*
* 1.0 ------------------ Events ----------------
*
************************************************/
// 1. Custom Post Type Registration (Events)
add_action( \'init\', \'create_event_postype\', 0 );
function create_event_postype() {
$labels = array(
\'name\' => _x(\'Events\', \'post type general name\'),
\'singular_name\' => _x(\'Event\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'events\'),
\'add_new_item\' => __(\'Add New Event\'),
\'edit_item\' => __(\'Edit Event\'),
\'new_item\' => __(\'New Event\'),
\'view_item\' => __(\'View Event\'),
\'search_items\' => __(\'Search Events\'),
\'not_found\' => __(\'No events found\'),
\'not_found_in_trash\' => __(\'No events found in Trash\'),
\'parent_item_colon\' => \'\',
);
$args = array(
\'label\' => __(\'Events\'),
\'labels\' => $labels,
\'public\' => true,
\'can_export\' => true,
\'show_ui\' => true,
\'_builtin\' => false,
\'_edit_link\' => \'post.php?post=%d\', // ?
\'capability_type\' => \'post\',
\'menu_icon\' => get_bloginfo(\'template_url\').\'/images/icons/events.png\',
\'hierarchical\' => false,
\'rewrite\' => array( "slug" => "events" ),
\'supports\'=> array(\'title\', \'thumbnail\', \'excerpt\', \'editor\', \'custom-fields\') ,
\'show_in_nav_menus\' => true,
\'taxonomies\' => array(\'post_tag\', \'tf_eventtype\')
);
register_post_type( \'tf_events\', $args);
}
// 2. Custom Taxonomy Registration (Event Types)
add_action( \'init\', \'create_eventtype_taxonomy\', 0 );
function create_eventtype_taxonomy() {
$labels = array(
\'name\' => _x( \'Event Type\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Event Type\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Event Types\' ),
\'popular_items\' => __( \'Popular Event Types\' ),
\'all_items\' => __( \'All Event Types\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Event Type\' ),
\'update_item\' => __( \'Update Event Type\' ),
\'add_new_item\' => __( \'Add New Event Type\' ),
\'new_item_name\' => __( \'New Event Type Name\' ),
\'separate_items_with_commas\' => __( \'Separate event types with commas\' ),
\'add_or_remove_items\' => __( \'Add or remove event types\' ),
\'choose_from_most_used\' => __( \'Choose from the most used event types\' ),
);
register_taxonomy(\'tf_eventtype\',\'tf_events\', array(
\'label\' => __(\'Event Type\'),
\'labels\' => $labels,
\'hierarchical\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'event-type\' ),
));
}
下面是一些伪代码,演示如何调用此函数
在自定义页面中,我通过AJAX调用函数(AJAX admin.php)
$.ajax({
type: "POST",
url: "<? bloginfo(\'url\'); ?>/wp-admin/admin-ajax.php", //Built in WP AJAX
data: data,
success: function(data){
//console.log(\'search complete\');
$("#event-list").html(data)
}
});
下面是调用自定义查询的函数,其中get\\u terms失败。
/****************************************************************
*
* 1.0 Calendar AJAX Queries
*
****************************************************************/
// if both logged in and not logged in users can send this AJAX request,
// add both of these actions, otherwise add only the appropriate one
add_action( \'wp_ajax_nopriv_calendar_search\', \'calendar_search\' );
add_action( \'wp_ajax_calendar_search\', \'calendar_search\' );
function calendar_search() {
$args = array(
\'post_type\' => \'tf_events\',
\'post_status\' => \'publish\',
\'order\' => \'ASC\'
);
$query = new WP_Query( $args );
// - loop start -
if ( $query->have_posts() ) {
while ($query->have_posts()) : $query->the_post(); /*Shows the posts that are available. */
//Event Type
$cats = get_the_terms($post->ID , \'tf_eventtype\'); //the function that fails. All other custom data works fine
echo
\'<article class="post" id="post-\'. $post->ID . \'" itemscope itemtype="http://schema.org/Event">
<time></time>
<h2 itemprop="name" class="entry-title"><a href="\' . get_permalink() .\'">\'. get_the_title() . \'</a></h2>
<div class="entry">
<p itemprop="location">\';
if($cats) foreach($cats as $cat){
echo " | " . $cat->name;
}
echo
"</p>
</div><!--#entry-->
</article>";
endwhile;
} else {
echo "<h2 class=\'month\'>Sorry, no events found!</h2>";
}
die(); // this is required to return a proper result
}