Support » Plugin: Custom Post Type UI » How to use cpt/%taxonomy% in rewrite rule

  • Resolved vgstef

    (@vgstef)


    Hi!

    I’m trying to set the “Custom Rewrite Slug” field for the custom post type to something like cpt/%taxonomy%

    But it’s not working. The url now directly shows the word “%taxonomy%” and doesn’t replace it with taxonomy term.

    Am I doing something wrong? In fact, is it possible to use %% in the Custom Rewrite Slug ?

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

    (@vgstef)

    Never mind! I found the solution.

    I had to add this in the file function.php

    ———

    add_filter('post_type_link', 'type_aliments_permalink_structure', 10, 4);
    function type_aliments_permalink_structure($post_link, $post, $leavename, $sample) {
        if (false !== strpos($post_link, '%type_aliments%')) {
            $type_aliments_type_term = get_the_terms($post->ID, 'type_aliments');
            if (!empty($type_aliments_type_term))
                $post_link = str_replace('%type_aliments%', array_pop($type_aliments_type_term)->
                slug, $post_link);
            else
                $post_link = str_replace('%type_aliments%', 'uncategorized', $post_link);
        }
        return $post_link;
    }
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    The only part that should go in the custom rewrite slug field for post types and taxonomies, is the part that you would want to be between the % characters. That’s because that’s all it’s meant to do, specify the value to use later on as you have in your code snippet above.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use cpt/%taxonomy% in rewrite rule’ is closed to new replies.