Support » Developing with WordPress » reverting custom permalink tag to default cat tag

  • Glenn


    Hi gang – need some help with a function, if you don’t mind.

    I’m using a custom cat tag, “industry,” and this function makes it work for my permalinks, but I’d like for the last conditional (else $taxonomy_slug = ‘blog’;) to allow WP to revert to a default category if no “industry” is selected, whereas this simply reverts to


    I tried %category% in place of ‘blog’, which did not work.

    Could someone kindly show me how this function can be amended to switch to the selected WP category if an “industry” is not selected …?

    with much gratitude…

    add_filter('post_link', 'industry_permalink', 10, 3);
    add_filter('post_type_link', 'industry_permalink', 10, 3);
    function industry_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%industry%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'industry');   
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'blog';
        return str_replace('%industry%', $taxonomy_slug, $permalink);
    • This topic was modified 2 years, 1 month ago by Glenn.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    The default category ID is stored as the option “default_category”. With that ID, get the term object with get_category(). Use the “slug” property of the returned object.

Viewing 1 replies (of 1 total)
  • The topic ‘reverting custom permalink tag to default cat tag’ is closed to new replies.