仅在页面永久链接中显示特定类别。

huangapple go评论64阅读模式
英文:

Show only specific categories in permalinks for pages

问题

我尝试在WordPress开发网络中提出这个问题,但没有成功。我正在尝试仅在永久链接中显示特定类别的页面。

现在,所有页面和帖子的永久链接结构(文章类型)是:

example.com/the-post-title/

这适用于我所有的帖子和页面类别,但我希望对于特定类别如下:

但我希望当我选择这些页面时,它显示如下类别 'downloads':

example.com/downloads/the-post-title/

有人能帮助我用PHP代码或其他方法实现这个特定类别的要求吗?

我尝试了一些插件并在网上搜索,但没有找到相关的信息。

英文:

I tried to ask this question in the WordPress Devlopment network with no success, I'm trying to display specific category only in permalinks for pages.

Right now the permalinks structure (POST Type) for All pages and post

example.com/the-post-title/

this is work for all my categories of post and pages and i want this for all categories look like
but i want this category 'downloads' when i select on those pages its show look like

example.com/downloads/the-post-title/

anyone help me out how to do it with php code or something else for this specific categories.

i try some plugin and search on web but not found anythinf related

答案1

得分: 0

你需要定义一个自定义的重写规则,以捕获对/downloads/的请求并正确地将其定向。为此,请使用add_rewrite_rule()来匹配现有的对文章和页面的重写规则。

然后使用 post_link 过滤器来更改文章的URL以包括 /downloads/

伪代码:

add_action( 'init', static function () {
    add_rewrite_rule( ... );
} );

add_filter( 'post_link', static function ( $permalink, $post ) {
    // 添加用于检查文章是否在分类中的测试
    return sprintf( '/downloads/%s', $post->post_name );
}, 10, 2 );
英文:

You will want to define a custom rewrite rule, to capture requests to /downloads/ and direct them correctly. To do so, use add_rewite_rule() to match the existing rewrite rule for posts and pages.

Then use the post_link filter to change the post's URL to include /downloads/.

Pseudo-code:

add_action( 'init', static function () {
    add_rewrite_rule( ... );
} );

add_filter( 'post_link', static function ( $permalink, $post ) {
    // add test for post is in category
    return sprintf( '/downloads/%s', $post->post_name );
}, 10, 2 );

答案2

得分: -1

你可以使用此插件自定义链接:https://www.tiny.cloud/docs/plugins/opensource/link/

英文:

You can use https://www.tiny.cloud/docs/plugins/opensource/link/ this plugin for customizing your link.

huangapple
  • 本文由 发表于 2023年6月12日 22:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定