英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论