0

I have created a custom post type portfolio with custom taxonomy Portfolio Category. I can create portfolio items and assign portfolio categories to them and save these custom posts. But what I need is the permalink to show domain.com/portfolio-category/portfolio-item-name. Portfolio Category is variable depending on the category I pick.

Now I only see domain.com/portfolio/item-name The name portfolio should be the name of the custom taxonomy I attach the custom post to, but it does not. Is just a fixed name from the CPT it seems. I would like urls like domain.com/design/client-a and domain.com/dev/client-b

Here is my code:

// Register Custom Taxonomy
function portfolio_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Portfolio Category', 'Taxonomy General Name', 'imagewize_portfolio_plugin' ),
        'singular_name'              => _x( 'Portfolio Category', 'Taxonomy Singular Name', 'imagewize_portfolio_plugin' ),
        'menu_name'                  => __( 'Category', 'imagewize_portfolio_plugin' ),
        'all_items'                  => __( 'All Items', 'imagewize_portfolio_plugin' ),
        'parent_item'                => __( 'Parent Item', 'imagewize_portfolio_plugin' ),
        'parent_item_colon'          => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
        'new_item_name'              => __( 'New Item Name', 'imagewize_portfolio_plugin' ),
        'add_new_item'               => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
        'edit_item'                  => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
        'update_item'                => __( 'Update Item', 'imagewize_portfolio_plugin' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'imagewize_portfolio_plugin' ),
        'search_items'               => __( 'Search Items', 'imagewize_portfolio_plugin' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'imagewize_portfolio_plugin' ),
        'choose_from_most_used'      => __( 'Choose from the most used items', 'imagewize_portfolio_plugin' ),
        'not_found'                  => __( 'Not Found', 'imagewize_portfolio_plugin' ),
    );
    $rewrite = array(
        'slug'                       => 'Portfolio Category',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'portfolio', array( 'Img_portfolio_cpt' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'portfolio_taxonomy', 0 );




if ( ! function_exists('img_portfolio_cpt') ) {

// Register Custom Post Type
function img_portfolio_cpt() {

    $labels = array(
        'name'                => _x( 'Portfolio Items', 'Post Type General Name', 'imagewize_portfolio_plugin' ),
        'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'imagewize_portfolio_plugin' ),
        'menu_name'           => __( 'Portfolio', 'imagewize_portfolio_plugin' ),
        'parent_item_colon'   => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
        'all_items'           => __( 'All Items', 'imagewize_portfolio_plugin' ),
        'view_item'           => __( 'View Item', 'imagewize_portfolio_plugin' ),
        'add_new_item'        => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
        'add_new'             => __( 'Add New', 'imagewize_portfolio_plugin' ),
        'edit_item'           => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
        'update_item'         => __( 'Update Item', 'imagewize_portfolio_plugin' ),
        'search_items'        => __( 'Search Item', 'imagewize_portfolio_plugin' ),
        'not_found'           => __( 'Not found', 'imagewize_portfolio_plugin' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'imagewize_portfolio_plugin' ),
    );
    $rewrite = array(
        'slug'                => 'portfolio',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    );
    $args = array(
        'label'               => __( 'Img_portfolio_cpt', 'imagewize_portfolio_plugin' ),
        'description'         => __( 'Imagewize Portfolio', 'imagewize_portfolio_plugin' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'author', 'thumbnail', ),
        'taxonomies'          => array( 'portfolio' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-portfolio',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'capability_type'     => 'page',
    );
    register_post_type( 'Img_portfolio_cpt', $args );

}

// Hook into the 'init' action
add_action( 'init', 'img_portfolio_cpt', 0 );

}
1

This might solve your issue, and frankly, rewrites are still a little mistery to me, so I would not be able to help you out there, but there are two major issues in your code which you should first sort out before you try to sort out your rewrites

  • Custom post type names (and for that matter, function names and custom taxonomy names) should be all small letters and words separated by underscores. Your custom post type name starts with a capital letters. This can result in some issues later

  • Your rewrite slug for your taxonomy is wrong. Have a look at it. Slugs should be lowercase and words should be separated by hyphens. Your rewrite slug currently is Portfolio Category which will not work. You have to try something like portfolio-category

Make these changes and remember to flush your rewrite rules on completion of these changes otherwise these changes won't take effect. Once this errors is corrected, then you can start looking at your rewrite problem

  • Thanks for the feedback! I think I found the solution. One issue I believe was the slug as you also mentioned. Other issue was that I needed to rewrite the ctp slug. The CPT naming does not seem the issue, but I will probably change this to. Adding solution I found asap. – rhand Mar 23 '15 at 8:28
1

I went through the two urls I added as comments, WordPress Codex documentation such as on register_taxonomy here some more and came up with this code that does seem to do the trick. Main addition is the added filter post_type_link to filter the cpt slug and load the term needed.

    // Register Custom Taxonomy
    function portfolio_taxonomy() {

        $labels = array(
            'name'                       => _x( 'Labels', 'Taxonomy General Name', 'imagewize_portfolio_plugin' ),
            'singular_name'              => _x( 'Label', 'Taxonomy Singular Name', 'imagewize_portfolio_plugin' ),
            'menu_name'                  => __( 'Labels', 'imagewize_portfolio_plugin' ),
            'all_items'                  => __( 'All Items', 'imagewize_portfolio_plugin' ),
            'parent_item'                => __( 'Parent Item', 'imagewize_portfolio_plugin' ),
            'parent_item_colon'          => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
            'new_item_name'              => __( 'New Item Name', 'imagewize_portfolio_plugin' ),
            'add_new_item'               => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
            'edit_item'                  => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
            'update_item'                => __( 'Update Item', 'imagewize_portfolio_plugin' ),
            'separate_items_with_commas' => __( 'Separate items with commas', 'imagewize_portfolio_plugin' ),
            'search_items'               => __( 'Search Items', 'imagewize_portfolio_plugin' ),
            'add_or_remove_items'        => __( 'Add or remove items', 'imagewize_portfolio_plugin' ),
            'choose_from_most_used'      => __( 'Choose from the most used items', 'imagewize_portfolio_plugin' ),
            'not_found'                  => __( 'Not Found', 'imagewize_portfolio_plugin' ),
        );
        $rewrite = array(
            'slug'                       => 'label',
            'with_front'                 => true,
            'hierarchical'               => true,
        );
        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'rewrite'                    => $rewrite,
        );
        register_taxonomy( 'label', array( 'Img_portfolio_cpt' ), $args );

    }

    // Hook into the 'init' action
    add_action( 'init', 'portfolio_taxonomy', 0 );



    if ( ! function_exists('img_portfolio_cpt') ) {

    // Register Custom Post Type
    function img_portfolio_cpt() {

        $labels = array(
            'name'                => _x( 'Portfolio Items', 'Post Type General Name', 'imagewize_portfolio_plugin' ),
            'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'imagewize_portfolio_plugin' ),
            'menu_name'           => __( 'Portfolio', 'imagewize_portfolio_plugin' ),
            'parent_item_colon'   => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
            'all_items'           => __( 'All Items', 'imagewize_portfolio_plugin' ),
            'view_item'           => __( 'View Item', 'imagewize_portfolio_plugin' ),
            'add_new_item'        => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
            'add_new'             => __( 'Add New', 'imagewize_portfolio_plugin' ),
            'edit_item'           => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
            'update_item'         => __( 'Update Item', 'imagewize_portfolio_plugin' ),
            'search_items'        => __( 'Search Item', 'imagewize_portfolio_plugin' ),
            'not_found'           => __( 'Not found', 'imagewize_portfolio_plugin' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'imagewize_portfolio_plugin' ),
        );
        $rewrite = array(
            'slug'                => '%label%',
            'with_front'          => true,
            'pages'               => true,
            'feeds'               => true,
        );
        $args = array(
            'label'               => __( 'Img_portfolio_cpt', 'imagewize_portfolio_plugin' ),
            'description'         => __( 'Imagewize Portfolio', 'imagewize_portfolio_plugin' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'author', 'thumbnail', ),
            'taxonomies'          => array( 'label' ),
            'hierarchical'        => true,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'menu_icon'           => 'dashicons-portfolio',
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'rewrite'             => $rewrite,
            'capability_type'     => 'page',
        );
        register_post_type( 'Img_portfolio_cpt', $args );

    }

    // Hook into the 'init' action
    add_action( 'init', 'img_portfolio_cpt', 0 );

    }
add_filter('post_type_link', 'portfolio_permalink_structure', 10, 4);
function portfolio_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%label%' ) ) {
        $label_term = get_the_terms( $post->ID, 'label' );
        $post_link = str_replace( '%label%', array_pop( $label_term )->slug, $post_link );
    }
    return $post_link;
}

Will have to check naming issues for CPTs as @Pieter Goosen mentioned. I also need to learn quite a bit more on custom post types, but did learn quite a bit today and achieved what was aimed for.

  • +1 for posting your solution. Just remember to return to your answer after the initial restriction is lifted and accept your own answer :-) – Pieter Goosen Mar 23 '15 at 8:39

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.