• Resolved Zen Gonzaga

    (@zen-gonzaga)


    add_action( 'init', 'register_taxonomy_story_category' );
    
    function register_taxonomy_story_category() {
    
        $labels = array(
            'name' => _x( 'Story Categories', 'story category' ),
            'singular_name' => _x( 'Story Category', 'story category' ),
            'search_items' => _x( 'Search Story Categories', 'story category' ),
            'popular_items' => _x( 'Popular Story Categories', 'story category' ),
            'all_items' => _x( 'All Story Categories', 'story category' ),
            'parent_item' => _x( 'Parent Story Category', 'story category' ),
            'parent_item_colon' => _x( 'Parent Story Category:', 'story category' ),
            'edit_item' => _x( 'Edit Story Category', 'story category' ),
            'update_item' => _x( 'Update Story Category', 'story category' ),
            'add_new_item' => _x( 'Add New Story Category', 'story category' ),
            'new_item_name' => _x( 'New Story Category Name', 'story category' ),
            'separate_items_with_commas' => _x( 'Separate story categories with commas', 'story category' ),
            'add_or_remove_items' => _x( 'Add or remove story categories', 'story category' ),
            'choose_from_most_used' => _x( 'Choose from the most used story categories', 'story category' ),
            'menu_name' => _x( 'Story Categories', 'story category' ),
        );
    
        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
    		'rewrite' => array( 'slug' => 'story', 'with_front' => false ),
            'query_var' => true
        );
    
        register_taxonomy( 'story_category', array('story'), $args );
    
    	/*global $wp_rewrite;
    
    	$wp_rewrite->extra_permastructs['story_category'] = array('%story_category%', EP_NONE);*/
    }
    
    add_action( 'init', 'register_cpt_story' );
    
    function register_cpt_story() {
    
        $labels = array(
            'name' => _x( 'Stories', 'story' ),
            'singular_name' => _x( 'Story', 'story' ),
            'add_new' => _x( 'Add New', 'story' ),
            'add_new_item' => _x( 'Add New Story', 'story' ),
            'edit_item' => _x( 'Edit Story', 'story' ),
            'new_item' => _x( 'New Story', 'story' ),
            'view_item' => _x( 'View Story', 'story' ),
            'search_items' => _x( 'Search Stories', 'story' ),
            'not_found' => _x( 'No stories found', 'story' ),
            'not_found_in_trash' => _x( 'No stories found in Trash', 'story' ),
            'parent_item_colon' => _x( 'Parent Story:', 'story' ),
            'menu_name' => _x( 'Stories', 'story' ),
        );
    
        $args = array(
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields' ),
            'taxonomies' => array( 'post_tag', 'story_category'  ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'menu_icon' => 'http://lab.thecolorcure.com/evofiction/wp-content/themes/fiction-pond/images/evo-wp-icon.png',
            'show_in_nav_menus' => false,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => array( 'hierarchical' => true, 'slug' => 'story/%category%', 'with_front' => false ),
            'capability_type' => 'post'
        );
    
        register_post_type( 'story', $args );
    }
    
    function my_post_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
        if ( strpos('%category%', $post_link) === 'FALSE' ) {
          return $post_link;
        }
        $post = get_post($id);
        if ( !is_object($post) || $post->post_type != 'story' ) {
          return $post_link;
        }
        $terms = wp_get_object_terms($post->ID, 'story_category');
        if ( !$terms ) {
          return str_replace('story/%category%', '', $post_link);
        }
        return str_replace('%category%', $terms[0]->slug, $post_link);
    }
    add_filter('post_type_link', 'my_post_type_link_filter_function', 1, 3);

    I used this to add custom post type called story.
    Permalinks : /%category%/%postname%/

    I wanted the url to be: /parent-category/one-sub-category/post-title
    Right Now it’s displaying: /story/parent-category/sub-category/
    it’s not working.. the post-title is not included and it shows 404.

    HELP ME PLEASE

  • The topic ‘Custom Posts Types URLS’ is closed to new replies.