WordPress.org

Forums

Custom post type category navigation (4 posts)

  1. osby
    Member
    Posted 4 years ago #

    Hi!

    I'm going crazy on this one! I've searched google and forums for days looking for a solution to get this up an running...

    My scenario is as follows:
    I've created a custom post type with three custom taxonomies attached to it.

    1 of these are a hierarchical (category) and the other two are tags.
    I want the hierarchical one to act like the default post categories does in the menu.

    As so:
    Cat 1 (http://localhost/aoc/tavlingar/alla/)
    - Sub cat 1 (http://localhost/aoc/tavlingar/tavlingar-mtb/)
    - Sub cat 2 (http://localhost/aoc/tavlingar/tavlingar-lvg/)
    and so on.
    The permalinks works just as I want them, but the content just shows up in archive.php and doesn't matter which sub-cat I'm in.

    I can't get it to pick up the archive-aoc_tavlingar.php either, it just fall backs to archive.php

    This is what my functions.php looks like.

    add_action( 'init', 'create_aoc_tavlingar_cpt' );
    function create_aoc_tavlingar_cpt() {
    	register_post_type( 'aoc_tavlingar',
    		array(
    			'labels' => array(
    				'name' => __( 'Tävlingar' ),
    				'singular_name' => __( 'Tävling' ),
    				'add_new' => __('Lägg till ny'),
    				'add_new_item' => __('Lägg till ny tävling'),
    				'edit' => __('Ändra'),
    				'edit_item' => __('Ändra tävling'),
    				'new_item' => __('Ny tävling'),
    				'view' => __('Visa tävling'),
    				'view_item' => __('Visa tävling'),
    				'search_items' => __('Sök tävling'),
    				'not_found' => __('Ingen tävling hittades'),
    				'not_found_in_trash' => __('Ingen tävling i papperskorgen')
    			),
    		'public' => true,
    		'menu_position' => 5,
    		'hierarchical' => false,
    		'query_var' => true,
    		'supports' => array('title','custom-fields','thumbnail'),
    		'rewrite' => array('slug' =>'tavling','with_front' => true),
    		'has_archive' => true,
    		'taxonomies' => true
    		)
    	);
    }
    
    add_action( 'init', 'taxonomy_aoc_tavlingar');
    function taxonomy_aoc_tavlingar() {
    	register_taxonomy(
    		'aoc_tavlingar_gren',
    		'aoc_tavlingar',
    			array(
    				'hierarchical' =>true,
    				'label' => 'Gren',
    				'query_var' => true,
    				'rewrite' => array('slug' =>'tavlingar','with_front' => true),
    				)
    		);
    }
    
    add_action( 'init', 'taxonomy_aoc_tavlingar_tag_typ');
    function taxonomy_aoc_tavlingar_tag_typ() {
    	register_taxonomy(
    		'aoc_tavlingar_typ',
    		'aoc_tavlingar',
    			array(
    				'hierarchical' =>false,
    				'label' => 'Typ av lopp',
    				'query_var' => true,
    				'rewrite' => array('slug' =>'tavlings_typ','with_front' => true),
    				)
    		);
    }
    
    add_action( 'init', 'taxonomy_aoc_tavlingar_tag_misc', 0);
    function taxonomy_aoc_tavlingar_tag_misc() {
    	register_taxonomy(
    		'aoc_tavlingar_misc',
    		'aoc_tavlingar',
    			array(
    				'hierarchical' =>false,
    				'label' => 'Övrigt',
    				'query_var' => true,
    				'rewrite' => array('slug' =>'tavlingar_ovrigt','with_front' => true),
    				)
    		);
    }

    ANY help is appreciated!

  2. David Gard
    Member
    Posted 4 years ago #

    I believe archives only work for post types, not taxonomies. you would be looking to create a template with the following names to work for your taxonomies as declared above -

    • 'aoc_taxonomy-tavlingar_gren.php'
    • 'aoc_taxonomy-tavlingar_typ.php'
    • 'aoc_taxonomy-tavlingar_misc.php'

    'archive-aoc_tavlingar.php' would work to display posts in your Custom Post Type in an archive.

  3. osby
    Member
    Posted 4 years ago #

    Thanks for the answer!

    I see, soo the menu item of "cat 1" (in my custom post type) would be looking for a taxonomy-template and not a category-template like the default post posttype categories?

    However, I can't get any of them to work. Not even the archive-aoc_tavlingar.php.

    This is my current permalink structure:

    Custom post type with rewrite - ....localhost/aoc/tavling (gives me 404)
    Taxonomy with categories - ...localhost/aoc/tavlingar (gives me 404)
    Taxonomy with categories/cat 1 - localhost/aoc/tavlingar/alla (gives me archive.php)

    Single custom post type - localhost/aoc/tavling/"name of post" - (gives me a fully working single post template and working nav menu current classes and all seems fine!)

  4. osby
    Member
    Posted 4 years ago #

    Hupp!

    Actually, when i renamed the aoc_tavlingar.php to tavlingar.php it actually finds it and displays it.

    "Taxonomy with categories - ...localhost/aoc/tavlingar (gives me 404)"
    NOT true anymore..

    But since I can't add this item in the menu without using custom links (which I guess will destroy the current_classes and whatnot)

    Therefor I have a category (alla) as the parent to all the other categories (mtb/cx/lvg etc)
    And I put that one i my top level menu and the others like sub-menus. And as I said, it all works nicely when I'm on a single CPT post.

    Lots of text and probably a lot of jibberish. Hope you get it!

Topic Closed

This topic has been closed to new replies.

About this Topic