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



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

我正在使用由插件预定义的自定义帖子类型。看起来几乎每个事件日历插件都使用自定义帖子类型来设置"事件"帖子类型。

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

例如,我有区域类别,例如我一直用于常规帖子的"东南",但我也希望能够将此类别分配给活动帖子,以便人们在查看"东南"类别存档时,他们可以看到常规帖子以及与该类别关联的活动帖子。

这可能吗?

提前感谢您的任何帮助

简单:

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

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

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

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

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

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

以下是函数代码的副本.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');

最新更新