提前注册Taxonomy,以便可以在unctions.php和admin-ajax.php中使用它

时间:2013-10-17 作者:Nate

这是对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      
}

3 个回复
SO网友:Nate

因此,由于G.M.的帮助,我能够更有效地调试问题,现在我知道init确实没有被调用。我开始从我的函数中剥离出所有无关的函数。php文件,我意识到我调用了两次do\\u action,因为我有许多函数通过admin ajax运行。php。do\\u操作调用只能使用一次!奇怪的是,这个bug是如何出现的!

$action = isset($_POST[\'action\']) ? $_POST[\'action\'] : null;

//These functions take an ajax call and then call the appropriate PHP function
// this hook is fired if the current viewer is not logged in
do_action( \'wp_ajax_nopriv_\' . $action );
do_action( \'wp_ajax_\' . $action );

SO网友:gmazzap

只需添加global $post; 使用前$post 变量:

while ($query->have_posts()) : $query->the_post();
  global $post;
  $cats = get_the_terms($post->ID , \'tf_eventtype\');
  //... and so on

SO网友:Fabri

出现“invalid taxonomy”消息的原因是,在进行ajax调用时,并没有注册所有分类法,因为所有插件都没有初始化。

解决方案是在初始化之后启动hook ajax,这样它就可以正常工作了

例如:。

        add_action( \'init\', function() {

            //add the ajax hook only when all the taxonomies have been registered
            add_action(\'wp_ajax_my_action\',  function() {

                $result = get_terms( \'my_taxonomy_name\' );
            }

        }, 99);

结束

相关推荐

修改Precision_Posts链接以使用AJAX

我正在编辑wp-includes/link-template.php 文件,并希望使用Ajax将帖子加载到我的ID中#box-right. 我知道有很多插件可以使用,但我使用自己的HTML和CSS布局,包括帖子(没有将我的网站与WordPress集成)。我正在使用AJAX加载网站中某些DIV中的内容,我需要更改以下代码以将帖子加载到我的box right DIV中。return \'<a href=\"\' . previous_posts( false ) . \"\\\" $attr>\"