Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a Custom Post Type named News that has a URL structure like this...

domain.com/news/news-post-name

I have also added a Custom Tag Taxonomy for my News Post Type.

Here is my Re-write settings for it...

$news_posts_tag_args = array(
        'rewrite' => array(
                'slug'                       => 'news-tag',
                'with_front'                 => false,
                'hierarchical'               => true,
        )
);

This makes mt News Tag have a URL like this...

domain.com/news-tag/news-TAG-name

However I would like my it to appear that I have tag under my News Post Type

So that it will instead be like this...

domain.com/news/tag/news-TAG-name

I am able to achieve this by using the Re-write code below...

function custom_taxonomies_rewrite(){
    add_rewrite_rule('^news/tag/([^/]*)/?','index.php?news_tag=$matches[1]','top');
}
add_action('init','custom_taxonomies_rewrite');

So far everything works great but I then have to make sure to edit anything that links to the News Tag Taxonomy to make sure it uses my new custom URL structure for that Taxonomy.

So I am hoping someone can help me with the next step of my process by helping to to hook into Tag links so maybe I can change the links more easily?

share|improve this question

1 Answer 1

If anyone else is looking for this in the future, the result I came up with is this one...

/* Filter Custom Taxonomy Tag HTML Links */
function custom_term_link_url($content) {
    $current_path = 'news-tag';
    $new_path = 'news/tag';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('term_link', 'custom_term_link_url');

It will run code on Taxonomy Links. I still need to add in functionality to work with more then 1 custom taxonomy but this is a good start and works for 1

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.