Wordpress:您可以将常规帖子类别分配给自定义帖子类型吗?

时间:2012-04-25 02:52:47

标签: wordpress categories custom-post-type

我今天第一次使用自定义帖子类型,所以请原谅我的无知。

我正在使用插件预定义的自定义帖子类型。看起来几乎每个Event Calendar插件都使用自定义帖子类型来设置“事件”帖子类型。

我想知道是否有办法使用我分配给常规帖子的常规类别,以分配给自定义事件帖子。

例如,我有区域类别,比如我一直用于常规帖子的“东南”,但我也希望能够将此类别分配给活动帖子,以便当人们看到“东南”时类别档案,他们可以看到与该类别相关的常规帖子和活动帖子。

这可能吗?

感谢您提前提供任何帮助

3 个答案:

答案 0 :(得分:1)

简单:

add_action( 'init', 'myfuncxx'); function myfuncxx() {
    register_taxonomy_for_object_type( 'category', 'custom_postttt_typee' );
}

答案 1 :(得分:0)

您可能希望使用WordPress功能register_taxonomy_for_object_type()将以下内容放入主题的 functions.php 文件中:

function add_categories_to_events() {
    register_taxonomy_for_object_type( 'post_tag', 'event' );
}
add_action( 'init', 'add_categories_to_events' );

答案 2 :(得分:0)

我在这里使用了代码和说明: http://wp.miragearts.com/allinone-event-calendar-events-blog-home-categories-tags/

我发现将这个代码添加到functions.php中,如果我创建两个类别(一个用于常规帖子,一个用于事件自定义帖子),具有完全相同的名称和slug,那么它基本上与拥有相同一类。

我认为这可能会减慢我的网站速度,但现在判断它是否会导致问题还为时尚早。

这是functions.php代码的副本:

// Add this to your theme's functions.php
function edit_my_query($query) {
  // Modify category and tag listings to include ai1ec events and all uses of the same term
  //  across event and post taxonomies
  //  ie live-music or arts whether they are event or post categories
  // also include ai1ec events in blog home and feeds
  if ( ( is_home() || is_feed() || is_category() || is_tag() ) 
          &&  empty( $query->query_vars['suppress_filters'] ) ) {
    // The 'suppress_filters' test above keeps your menus from breaking
    $post_type = get_query_var('post_type');
    if($post_type && $post_type[0] != 'post') {
      $post_type = $post_type;
    } else {
      $post_type = array('post','ai1ec_event'); // add custom post types here
    }
    $query->set('post_type',$post_type);
    if (is_category() || is_tag()) {
    // Add custom taxonomies to category and tag pages
    if (is_category()) {
        $taxonomy1 = 'category';
        $taxonomy2 = 'events_categories';
      }
      if (is_tag()){
        $taxonomy1 = 'post_tag';
        $taxonomy2 = 'events_tags';
      }
      $queried_object = $query->get_queried_object();
      $slug = $queried_object->slug;
      $query->set('tax_query', array(
        'relation' => 'OR',
        array(
          'taxonomy' => $taxonomy1,  'field' => 'slug', 'terms' => $slug
        ),
        array(
          'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
        )
      ));
    }
  }
}
add_action('pre_get_posts', 'edit_my_query');
相关问题