I'm currently using the Avada Theme that is making use of custom post types being Portfolio Categories & Portfolio Items (which are part of one or more categories).

The theme is offering the solution to use a fixed slug "prefix" (as can be seen here). This solution translates into an url like this: www.domain.com/fixed-prefix-here/portfolio-item-title

What I would like to attempt, is to set the fixed prefix dynamically, setting it to the category of that custom post.

F.e. the slug of the portfolio item "test", which has as category "category-test" should be: www.domain.com/category-test/test

I'm fairly new to WordPress development, not to PHP. Any help is welcome. Thank you !


Try #1 I set the "fixed slug" to a custom taxonomy %portfolio_prefix% and replacing it in a function after saving that custom taxonomy (see code snippet). This works, the custom url's are shown for single posts, and posts load correctly when opened. Unfortunately my single pages crashes when I open them for some reason. Any tips welcome.

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

// The post_link hook allows us to translate tags for regular post objects
add_filter('post_link', 'portfolio_permalink', 10, 3);

// The post_type_link hook allows us to translate tags for custom post type objects
add_filter('post_type_link', 'portfolio_permalink', 10, 3);

function add_custom_taxonomies()
    register_taxonomy('portfolio_prefix', 'post', array(
        'hierarchical' => false,
        'labels' => array(
            'name' => _x('Portfolio prefix', 'taxonomy general name') ,
            'singular_name' => _x('Portfolio prefix', 'taxonomy singular name') ,
            'search_items' => __('Portfolio prefix search') ,
            'all_items' => __('All Portfolio prefix') ,
            'edit_item' => __('Edit Portfolio prefix') ,
            'update_item' => __('Update Portfolio prefix') ,
            'add_new_item' => __('Add New Portfolio prefix') ,
            'new_item_name' => __('New Portfolio prefix Name') ,
            'menu_name' => __('Portfolio prefixs') ,
        ) ,
        'rewrite' => array(
            'slug' => 'portfolio_prefix', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => false

        ) ,

function portfolio_permalink($permalink, $post_id) {
    // If the permalink does not contain the %portfolio_prefix% tag, then we don’t need to translate anything.
    if (strpos($permalink, '%portfolio_prefix%') === FALSE) return $permalink;

    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

    // Get the portfolio_prefix terms related to the current post object.
    $terms = wp_get_object_terms($post->ID, 'portfolio_category');

    // Retrieve the slug value of the first portfolio_prefix custom taxonomy object linked to the current post.
    if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
        $taxonomy_slug = $terms[0]->slug;
    // If no portfolio_prefix terms are retrieved, then replace our portfolio_prefix tag with the value "portfolio_prefix"
        $taxonomy_slug = 'portfolio_category';

    // Replace the %portfolio_prefix% tag with our custom taxonomy slug
    return str_replace('%portfolio_prefix%', $taxonomy_slug, $permalink);

I think your solution is here Include Taxonomy slug in post url

Also, you can try to play with the permalink structure in Settings->Permalinks WordPress admin menu, but I'm not sure that it helps you.

  • Hi Oleh. I tried to implement the function from your link, which has worked, though my pages now are crashing (404's). I guess, as some posts are used as snippets in some pages, the function tries to rework the pages too, but that should not happen I think. Any tips?
    – SolidTerre
    May 2 '19 at 21:43
  • You can try to update the permalinks structure after adding this code, just go to Settings->Permalinks and press Update button, when you change URL structure you need to do this - something like WordPress issue.
    – Oleh
    May 2 '19 at 21:50
  • I guessed that by now :-) but still, regenerating permalinks does not repair my pages for some reason... :-( Permalinks of the custom posts are working fine, it's just the pages that now refer to 404 pages.
    – SolidTerre
    May 2 '19 at 21:57

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.