Custom Post Type中的URL适用于术语,但不适用于其分类

时间:2015-12-31 作者:Boris Kozarac

我为一个自定义帖子类型和一些术语创建了12个分类法。在创建新帖子时,它看起来像这样,那是完美的-Taxonomies with terms

我有:

归档页面-url为sitename/kalendar-正常工作-单页面-url为sitename/kalendar/title-正常工作-分类页面-url应为sitename/kalendar/january、sitename/kalendar/Fenuary等-不工作,错误404,我需要修复此术语页面-url为sitename/kalendar/january/oranges,sitename/kalendar/二月/橙色等-正在工作,但我想禁用它如何修复我的URL?

我的完整代码在这里,我已经设置好了,如果有人想测试它,他可以很容易地把它放在插件文件夹中并激活。

<?php
/*
Plugin Name: _myctptitle
Plugin URI: -
Description: _myctptitle
Version: 1.0
Author: bk
Author URI: -
License: GPLv2
*/


class kalendar {

    public  $months = array(
        \'January\' => \'january\', \'February\' => \'february\' , \'March\' => \'march\' , \'April\'  => \'april\',
        \'May\'=> \'may\' , \'June\' => \'june\' , \'July\'=>\'july\'  , \'August\' => \'august\' , 
        \'September\' => \'september\', \'October\' =>\'oktober\' , \'November\'=> \'november\', \'December\' => \'december\'
        );
    public  $terms = array(\'apples\', \'oranges\', \'bananas\', \'sour gummy bears\');

    public function __construct()   {
        register_activation_hook( __FILE__,array( $this,\'activate\' ) );
        add_action( \'init\', array( $this, \'register_post_type\' ) );
    }

    public function activate()  {
        $this->register_post_type();
        $this->register_terms();
    }
    public function register_post_type()    {


        // register taxonomies - every month
        foreach($this->months as $month => $month_var) :
            $args = array(
                \'labels\' => array(\'name\' => $month),
                \'hierarchical\' => true,
                \'rewrite\' => array(
                    \'slug\' => \'kalendar/\'.$month_var,
                    \'hierarchical\' => true
                    ),
                \'query_var\' => $month_var
                );
        register_taxonomy($month_var, \'kalendar\', $args);       
        endforeach;


        // register post type
        $args = array(
            \'labels\' => array(
                \'name\' => \'Kalendar\',   
                ),
            \'query_var\' => \'kalendar\',
            \'rewrite\' => array(
                \'slug\' => \'kalendar\'
                ),
            \'public\' => true,
            \'has_archive\' => true,
            \'menu_position\' => 5,
            \'hierarchical\' => false,
            \'exclude_from_search\' => false,
            \'supports\' => array(\'title\', \'excerpt\')
            );
        register_post_type(\'kalendar\', $args);

        $this->register_terms();


    }

    public function register_terms() {
        foreach($this->months as $months => $slug){
            foreach($this->terms as $term)  {
                if(!term_exists($term, $slug))
                    wp_insert_term($term, $slug);
            }
        }
    }



}

$kalendar = new kalendar();

1 个回复
SO网友:Mihir

如果您将[taxonomy\\u name]注册为

register_taxonomy(\'kalendar\', [taxonomy_name], $args );
所以试试这个

foreach($this->months as $month => $month_var) :
  $args = array(
    \'labels\' => array(\'name\' => $month),
    \'hierarchical\' => true,
    \'rewrite\'      => array( \'slug\' => $month_var ),
    \'query_var\' => true
  );
  register_taxonomy($month_var, \'kalendar\', $args);       
endforeach;
然后保存永久链接设置以刷新重写规则。