1

I've made a custom post type called 'portfolio' and a custom taxonomy called 'portfolio-categorie'. While I see the new categories in my pages, I have a problem removing the 'front' from the url structure. My preferred url would be domain.com/portfolio/category-name/ or domain.com/portfolio/categorie/catergory-name would also be an option.

In my permalink settings I have this as a structure (used for blog): /blog/%postname%/

When I visit a custom taxonomy category page, I get a 404 error page. I did rewrite the permalinks.

If I change the with_front to 'with_front' => false WordPress returns a page but with the url /blog/portfolio/portfolio-name/

What am I doing wrong?

My code so far:

function custom_post_type() {

    $labels = array(
        'name'                  => _x( 'Portfolio', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Project', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Portfolio', 'text_domain' ),
        'name_admin_bar'        => __( 'Portfolio', 'text_domain' ),
        'archives'              => __( 'Item Archives', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'Alle Projecten', 'text_domain' ),
        'add_new_item'          => __( 'Voeg nieuw Project toe', 'text_domain' ),
        'add_new'               => __( 'Voeg toe', 'text_domain' ),
        'new_item'              => __( 'Nieuw Project', 'text_domain' ),
        'edit_item'             => __( 'Bewerk Project', 'text_domain' ),
        'update_item'           => __( 'Pas Project aan', 'text_domain' ),
        'view_item'             => __( 'Bekijk Project', 'text_domain' ),
        'search_items'          => __( 'Doorzoek Project', 'text_domain' ),
        'not_found'             => __( 'Niet gevonden', 'text_domain' ),
        'not_found_in_trash'    => __( 'Niet gevonden in de prullenmand', 'text_domain' ),
        'featured_image'        => __( 'Project Hoofdafbeelding', 'text_domain' ),
        'set_featured_image'    => __( 'Stel Hoofdafbeelding in', 'text_domain' ),
        'remove_featured_image' => __( 'Verwijder hoofdafbeelding', 'text_domain' ),
        'use_featured_image'    => __( 'Gebruik als hoofdafbeelding', 'text_domain' ),
        'insert_into_item'      => __( 'Voeg toe aan project', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Geupload naar dit project', 'text_domain' ),
        'items_list'            => __( 'Project lijst', 'text_domain' ),
        'items_list_navigation' => __( 'Project navigatie', 'text_domain' ),
        'filter_items_list'     => __( 'Filter project lijst', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Portfolio', 'text_domain' ),
        'description'           => __( 'Recente projecten', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', ),
        'hierarchical'          => true,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-images-alt',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
        'rewrite'               => array( 'with_front' => false ),
    );
    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'custom_post_type', 0 );


function portfolio_custom_taxonomy() {

  $labels = array(
    'name' => _x( 'Categorieën', 'taxonomy general name' ),
    'singular_name' => _x( 'Categorie', 'taxonomy singular name' ),
    'search_items' =>  __( 'Zoek categorieën' ),
    'all_items' => __( 'Alle categorieën' ),
    'parent_item' => __( 'Bovenliggende categorie' ),
    'parent_item_colon' => __( 'Bovenliggende categorie:' ),
    'edit_item' => __( 'Bewerk categorie' ), 
    'update_item' => __( 'Update categorie' ),
    'add_new_item' => __( 'Nieuwe categorie toevoegen' ),
    'new_item_name' => __( 'Nieuwe categorie' ),
    'menu_name' => __( 'Categorieën' ),
  );    

  register_taxonomy('portfolio-categorie', 'portfolio', array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
  ));
}

add_action( 'init', 'portfolio_custom_taxonomy', 0 );

Any help would be greatly appreciated!

0

To get the URL like example.tld/portfolio/PORTFOLIO-CATEGORY-NAME/PORTFOLIO-NAME/ do the following.

Change rewrite for the custom post type to

'rewrite' => array(
    'slug'       => 'portfolio/%portfolio-categorie%',
    'with_front' => false
),

Change rewrite for the custom taxonomy to

'rewrite' => array(
    'with_front' => false
),

Use post_type_link filter to replace %portfolio-categorie% with the actual taxonomy term:

add_filter( 'post_type_link', 'my_portfolio_permalink', 10, 4 );

function my_portfolio_permalink( $post_link, $post, $leavename, $sample ) {
    if ( false !== strpos( $post_link, '%portfolio-categorie%' ) ) {

        $portfolio_cat_term = get_the_terms( $post->ID, 'portfolio-categorie' );

        if ( !empty( $portfolio_cat_term ) ) {
            $post_link = str_replace( '%portfolio-categorie%', array_pop( $portfolio_cat_term )->
        slug, $post_link );
        } else {
            $post_link = str_replace( '%portfolio-categorie%', 'uncategorized', $post_link );
        }
    }
    return $post_link;
}

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.