• I’m creating a custom post type with taxonomies in WordPress 4.7.3, but for some reason the taxonomy pages aren’t working properly. My custom post type is “events”, but the archive-events.php template file isn’t working when visiting “mysite.com/events/event-category” or when visiting “mysite.com/events”

    Below is my code from functions.php, what am I doing wrong here??? lol

    add_action( 'init', 'register_events', 20 );
    function register_events() {
        $labels = array(
            'name' => _x( 'All Events', 'events','sonal' ),
            'singular_name' => _x( 'Event', 'events', 'sonal' ),
            'add_new' => _x( 'Add New', 'events', 'sonal' ),
            'add_new_item' => _x( 'Add New Event', 'events', 'sonal' ),
            'edit_item' => _x( 'Edit Event', 'events', 'sonal' ),
            'new_item' => _x( 'New Event', 'events', 'sonal' ),
            'view_item' => _x( 'View Event', 'events', 'sonal' ),
            'search_items' => _x( 'Search Events', 'events', 'sonal' ),
            'not_found' => _x( 'No Events found...', 'events', 'sonal' ),
            'not_found_in_trash' => _x( 'No Events found in Trash', 'events', 'sonal' ),
            'parent_item_colon' => _x( 'Parent Event:', 'events', 'sonal' ),
            'menu_name' => _x( 'Events', 'events', 'sonal' ),
        );
    
        $args = array(
            'labels' => __( $labels, 'local' ),
            'hierarchical' => true,
            'description' => 'events',
            'supports' => array( 'title', 'editor', 'excerpt', 'author', 'revisions' ),
            'taxonomies' => array( 'events_category'),
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'menu_icon' => 'dashicons-tickets-alt',
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
            'public' => true,
            'has_archive' => 'events_category',
            'capability_type' => 'post'
        );  
        register_post_type( 'events', $args );
    }  
    //Create Taxonomies (Categories)
    add_action( 'init', 'create_events_taxonomies', 20 );
    function create_events_taxonomies() {
    	$labels = array(
    		'name'              => _x( 'Event Categories', 'taxonomy general name', 'sonal' ),
    		'singular_name'     => _x( 'Event Category', 'taxonomy singular name', 'sonal' ),
    		'search_items'      => __( 'Search Event Categories', 'sonal' ),
    		'all_items'         => __( 'All Event Categories', 'sonal' ),
    		'parent_item'       => __( 'Parent Event Category', 'sonal' ),
    		'parent_item_colon' => __( 'Parent Event Category:', 'sonal' ),
    		'edit_item'         => __( 'Edit Event Category', 'sonal' ),
    		'update_item'       => __( 'Update Event Category', 'sonal' ),
    		'add_new_item'      => __( 'Add New Event Category', 'sonal' ),
    		'new_item_name'     => __( 'New Event Category Name', 'sonal' ),
    		'menu_name'         => __( 'Event Categories', 'sonal' ),
    	);
    
    	$args = array(
    		'hierarchical'      => true,
    		'labels'            => $labels,
    		'show_ui'           => true,
    		'show_admin_column' => true,
    		'query_var'         => true,
    		'rewrite'           => array( 'slug' => 'events', 'with_front' => false ),
    	);
    
    	register_taxonomy( 'events_category', array( 'events' ), $args );
    }
    //Set Permalinks
    function wpa_events_permalinks( $post_link, $post ){
        if ( is_object( $post ) && $post->post_type == 'events' ){
            $terms = wp_get_object_terms( $post->ID, 'events_category' );
            if( $terms ){
                return str_replace( '%events_category%' , $terms[0]->slug , $post_link );
            }
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'wpa_events_permalinks', 1, 2 );
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your rewrite slug and has_archive arguments are conflicting. They’re essentially the same argument in two different ways. Make has_archive simply true, so that the rewrite slug will be used.

    The rewrite slug is just that, a slug. You are trying to use it like a permastruct. The value should just be 'events' or whatever you want to see in the permalinks.

    Even with those straightened out, the URL mysite.com/events/event-category/ is not going to be supported unless you add a rewrite rule for it. The events archive is requested with mysite.com/events/ And the taxonomy archive URL will always need a term parameter as well. Just mysite.com/event-category/ will not work, there must be a term, like mysite.com/event-category/local/

Viewing 1 replies (of 1 total)
  • The topic ‘Trouble with Custom Post Type…’ is closed to new replies.