I’ve got a question. Maybe it’s something existing and I don’t know the name. Or maybe it’s something custom.
I’ve got a post type for news. The news posts have categories. These are some examples of separate news items
News item 1.
- Category “purple”
News item 2.
- Category “purple”
- Category “red”
News item 3.
- Category “red”
I have 3 pages where I show and filter the news:
- www.domain/news (This shows all the news)
- www.domain/purple/news (This shows all the purple news)
- www.domain/red/news (This shows all the purple news)
Showing a filtered post grid on each news page is not the problem. The problem is displaying them. By default the permalinks of each news post is:
- www.domain/news/news-item-1
- www.domain/news/news-item-2
- www.domain/news/news-item-3
And this is fine for the news on www.domain/news/
But now I want to display the category news items like following: From www.domain/purple/news I want to click on the news item and open it like:
- www.domain/purple/news/news-item-1
- www.domain/purple/news/news-item-2
And from www.domain/red/news I want to click on the news item and open it like:
- www.domain/red/news/news-item-2
- www.domain/red/news/news-item-3
The Question
How do I get the news on the category news pages to open and show the items on the preferred url? (this should also show up in the sitemap.xml)
I’ve got a question. Maybe it’s something existing and I don’t know the name. Or maybe it’s something custom.
I’ve got a post type for news. The news posts have categories. These are some examples of separate news items
News item 1.
- Category “purple”
News item 2.
- Category “purple”
- Category “red”
News item 3.
- Category “red”
I have 3 pages where I show and filter the news:
- www.domain/news (This shows all the news)
- www.domain/purple/news (This shows all the purple news)
- www.domain/red/news (This shows all the purple news)
Showing a filtered post grid on each news page is not the problem. The problem is displaying them. By default the permalinks of each news post is:
- www.domain/news/news-item-1
- www.domain/news/news-item-2
- www.domain/news/news-item-3
And this is fine for the news on www.domain/news/
But now I want to display the category news items like following: From www.domain/purple/news I want to click on the news item and open it like:
- www.domain/purple/news/news-item-1
- www.domain/purple/news/news-item-2
And from www.domain/red/news I want to click on the news item and open it like:
- www.domain/red/news/news-item-2
- www.domain/red/news/news-item-3
The Question
How do I get the news on the category news pages to open and show the items on the preferred url? (this should also show up in the sitemap.xml)
Share Improve this question asked Nov 29, 2018 at 10:23 TimTim 1671 silver badge7 bronze badges 4 |2 Answers
Reset to default 0you can use Custom Permalinks plugin. you can create and edit each post and category link very essay. https://wordpress/plugins/custom-permalinks/ Try this once
I've found a solution. This is the abstracted version:
add_rewrite_rule(
'^red\/news([^/]*)/?',
'index.php?event=$matches[1]',
'top'
);
//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules( false );
In the actual function I've made an array with the pages I want to redirect. (Source and destination) and run foreach and flush the rules after.
example/news/purple/
andexample/news/purple/first-news-slug/
instead? (It's much more natural and intuitive...) – Krzysiek Dróżdż Commented Nov 29, 2018 at 10:58