• Resolved tgelles

    (@tgelles)


    Wonderful plugin!

    Though how do you place a Custom Taxonomy in the slug of a Custom Post Type?

    I’ve attached my custom taxonomy to my custom post type, but I can only access the taxonomies when I use the taxonomy name (‘classification’) in the url, not the custom post types.

    For example, my CPT is dr12. My Taxonomy is spectro. I get a 404 if my url is “~/dr12/spectro”. But if it’s “~/classification/spectro” it loads.

    Thoughts?

    https://www.ads-software.com/plugins/wck-custom-fields-and-custom-post-types-creator/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tgelles

    (@tgelles)

    Let me rephrase:

    My CPT is DR12. Any post in that CPT is rendered as “~/dr12/{post-title}”.

    My Custom Taxonomy is “spectro”. How do I get it so any post assigned to that CTC get spectro added to the URL so it reads: “~/dr12/spectro/{post-title}”

    N.B.: I only want this to occur when I specifically assign posts to that taxonomy. Otherwise I don’t want any additional slug to appear (i.e. keep it at “~/dr12/{post-title}”).

    Plugin Author Cristian Antohe

    (@sareiodata)

    Hi,

    I’ve already answered you via email, but I’ll post it here also for other people who might have the same problem.

    Basically that’s not possible by default with WordPress. You might have a look at this forum post: https://www.ads-software.com/support/topic/insert-category-of-taxonomy-between-custom-post-type-and-post

    We do have support for the Rewrite Slug in WCK (in the Advanced Options when you create a CPT), but it’s not dynamic (unless you custom code it).

    In the post above you’ll see code like:

    /*Filtro per modificare il permalink*/
    add_filter('post_link', 'brand_permalink', 1, 3);
    add_filter('post_type_link', 'brand_permalink', 1, 3);
    
    function brand_permalink($permalink, $post_id, $leavename) {
    	//con %brand% catturo il rewrite del Custom Post Type
        if (strpos($permalink, '%brand%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'brand');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            	$taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'no-brand';
    
        return str_replace('%brand%', $taxonomy_slug, $permalink);
    }

    Basically he’s replacing the %brand% with the actual term slug.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Taxonomy in CPT slug’ is closed to new replies.