0

I have created 12 taxonomies for a custom post type and a few terms. On creating new post it looks like this and that is perfect - Taxonomies with terms

I have:

  • archive page - url is sitename/kalendar - working ok
  • single page - url is sitename/kalendar/title - working ok
  • taxonomy page - url should be sitename/kalendar/january, sitename/kalendar/february etc., - IT IS NOT WORKING, error 404, I need to fix this
  • term page - url is sitename/kalendar/january/oranges, sitename/kalendar/february/orange etc. - working BUT I want to disable it

How do I fix my urls?

My full code is here, I have set it up so that if anyone wants to test it, he can easily put it in a plugin folder and activate.

<?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();
  • What is a taxonomy page exactly? What would you expect to see at sitename/kalendar/january? The built in category taxonomy sitename/category is also a 404, that's how taxonomies work. – Milo Dec 31 '15 at 21:45
  • Hm I didn't know that... Having that kind of url structure would mean tremendously, because for every month I could have a list of posts whose terms belong to that month. – Boris Kozarac Dec 31 '15 at 22:35
  • Do you have any hints how can I accomplish that? I don't mind writing custom WP_Query, I just need to have proper url structure and a file where to write the code. I have look into wordpress.stackexchange.com/questions/127252/… and it works nice if I add manually 12 months as posts, but the problem with this is that it messes wp_count_posts( 'myctp' )->publish; – Boris Kozarac Jan 1 '16 at 11:17
  • Hm, I could do wp_count_posts( 'myctp' )->publish-12 though... – Boris Kozarac Jan 1 '16 at 11:23
  • Normally you would have to add rewrite rules, but the other issue you have is that sitename/kalendar/title and sitename/kalendar/january are the same URL pattern. The single kalendar post rules already exist to make those queries for post titles. You will have to hook query parsing and figure out if the request is supposed to be for a term or a post title, and manipulate the query vars accordingly. If you search for "last URI segment" you might find another question I answered about how to do this. You'll make your life easier by making the URLs more unique though. – Milo Jan 1 '16 at 17:28
1

[taxonomy_name] should be available in permalink automatically if you register it as

register_taxonomy('kalendar', [taxonomy_name], $args );

So Try this

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;

Then Save Permalink Settings to flush rewrite rules.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.