【问题标题】:Wordpress: Can you assign the regular post categories to custom post types?Wordpress:您可以将常规帖子类别分配给自定义帖子类型吗?
【发布时间】:2012-04-25 02:52:47
【问题描述】:

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

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

我想知道是否有一种方法可以使用我分配给常规帖子的正常类别来分配给自定义事件帖子。

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

这可能吗?

提前感谢您的帮助

【问题讨论】:

  • 是的,这是可能的,但取决于您用于定义帖子类型的插件。那么这个插件是什么?
  • 我还没有确定一个活动插件,但我可能会使用一体化活动日历

标签: wordpress categories custom-post-type


【解决方案1】:

简单:

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

【讨论】:

    【解决方案2】:

    您可能想使用 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' );
    

    【讨论】:

    • 我试过这个,得到了这个错误:警告:call_user_func_array() [function.call-user-func-array]:第一个参数应该是一个有效的回调,'add_category_to_events'在第 405 行的 /homepages/0/d328057274/htdocs/cfsa-test/wp-includes/plugin.php 中给出 我也尝试使用它,但将“事件”更改为“ai1ec-events”,因为我正在使用多合一活动日历。相同的错误信息。我在 Wordpress.org 上找到了这篇文章,我将尝试按照指示进行操作:wordpress.org/support/topic/…
    【解决方案3】:

    我在这里使用了代码和说明: 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2011-11-19
      • 2017-03-18
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多