php - News post with multiple permalinks? When opened from different page

admin2025-06-05  1

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
  • And why do you want to reverse order of slugs in the URL? Why not use example/news/purple/ and example/news/purple/first-news-slug/ instead? (It's much more natural and intuitive...) – Krzysiek Dróżdż Commented Nov 29, 2018 at 10:58
  • @KrzysiekDróżdż, because example/purple is sort of a landing page for this category. With a new page and some other pages under it. – Tim Commented Nov 29, 2018 at 12:22
  • Posts should have a single canonical URL, otherwise search engines will penalize you for duplicate content. – Milo Commented Nov 29, 2018 at 14:31
  • @Milo, thanks for bringing that to my attention. I'll use "rel="canonical" link element" for this duplicates to fix it. Source: support.google/webmasters/answer/66359?hl=en – Tim Commented Nov 29, 2018 at 14:47
Add a comment  | 

2 Answers 2

Reset to default 0

you 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749134525a316655.html

最新回复(0)