[Solved] Using the slug of a custom post category as prefix for the underlying custom post slugs

SolidTerre Asks: Using the slug of a custom post category as prefix for the underlying custom post slugs
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);

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.