从自定义帖子类型的帖子网址
-
-
我想我为什么要这么做呢?困惑.I guess I'm scratching my head as to why you would want to do that? Confused.
- 0
- 2017-05-29
- Michael Ecklund
-
@MichaelEcklund,因为用于创建面向公众的网页的任何CPT在URL中都有一个强制的子弹名称.实际上,有许多wp开发人员希望安全地删除该段塞.@MichaelEcklund because any CPT that is used to create public facing web pages has a forced slug name in the URL. There is actually a lot of wp devs looking to remove the slug safely.
- 3
- 2017-07-18
- Ben Racicot
-
10 个回答
- 投票数
-
- 2015-09-30
以下代码将起作用,但是您只需要记住,如果您自定义帖子类型的信息与页面或帖子的信息相同,则冲突很容易发生...
首先,我们将从永久链接中删除该子弹:
function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; } $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
仅仅去除弹头是不够的.现在,您将获得404页面,因为WordPress只希望帖子和页面具有这种行为.您还需要添加以下内容:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'events', 'page' ) ); } } add_action( 'pre_get_posts', 'na_parse_request' );
只需将"事件"更改为自定义帖子类型,就可以了.您可能需要刷新永久链接.
The following code will work, but you just have to keep in mind that conflicts can happen easily if the slug for your custom post type is the same as a page or post's slug...
First, we will remove the slug from the permalink:
function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; } $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
Just removing the slug isn't enough. Right now, you'll get a 404 page because WordPress only expects posts and pages to behave this way. You'll also need to add the following:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'events', 'page' ) ); } } add_action( 'pre_get_posts', 'na_parse_request' );
Just change "events" to your custom post type and you're good to go. You may need to refresh your permalinks.
-
谢谢.您是否认为这比手动创建重写更好?我已经看到了这种解决方案,它可能会使您提到的冲突无法解决?thanks. Do you think this is better than creating the rewrites manually? I've seen that solution and it may keep the conflicts you mention at bay?
- 0
- 2015-10-01
- Ben Racicot
-
如果您确信不会造成冲突,那么我认为这是一个很好的自动化解决方案.如果您要提供此功能,则这不是一个很好的解决方案.假设某位客户不是精通技术.I think this is a good automated solution if you're confident that you won't be creating conflicts. This isn't a good solution if you're providing this to... let's say a client that isn't tech savvy.
- 0
- 2015-10-01
- Nate Allen
-
您能否更新,如何将此代码用于多种帖子类型can you please update, how to use this code for multiple post types
- 1
- 2016-01-25
- Abin
-
nginx失败,因为条件`2!=count($ query-> query)`.使用nginx,您可以将$ query-> query作为'array('page'=>'','name'=>'...','q'=>'...')那么@NateAllen,该条件的含义是什么?It fails with nginx because the condition `2 != count( $query->query )`. With nginx, you can have $query->query as `array('page' => '', 'name' => '...', 'q' => '...')`. So @NateAllen, what is the meaning of that condition?
- 1
- 2016-11-08
- Fabio Montefuscolo
-
我们需要比这更好的东西.支持删除内置的Slug,以便以后我们无法创建有冲突的URL.常规帖子和页面创建其URL的方式.We need something better than this. Support to remove the slug built in so that we cannot create conflicting URLs later on. The way regular posts and pages create their URLs.
- 3
- 2017-07-18
- Ben Racicot
-
就是我还是这会破坏某些wordpress条件标签,例如is_single()和is_singular()?Is it just me or does this break some wordpress conditional tags like is_single() and is_singular()?
- 4
- 2017-07-18
- rob-gordon
-
不幸的是,此解决方案导致一些链接断开,我的博客停止显示帖子,只是一个正常页面.请参阅下面的Matt Keys提供的更好解决方案.This solution unfortunately caused some broken links and my blog stopped showing posts and was just a normal page. See a better solution below by Matt Keys.
- 1
- 2018-10-08
- Radley Sustaire
-
该代码假定`post_type`名称与自定义帖子类型`slug`相同,不一定在每种情况下都必须相同.但是对于其余部分,虽然我同意使用本地WP解决方案会更好,但还是有很好的解决方案.This code assumes that the `post_type` name is the same as the custom post type `slug` which doesn't necessarily have to be in every case. But for the rest, great solution although I agree a native WP solution would be better.
- 0
- 2019-06-14
- Marco Miltenburg
-
single- {cpt} .php使用此方法停止工作single-{cpt}.php stops working using this approach
- 0
- 2020-01-05
- Saleh Mahmood
-
对于那些对上面的代码有疑问的人,如果将第二个函数(**functionna_parse_request()**)替换为此[answer](https://wordpress.stackexchange.com)上的一个函数,它的工作原理就像一种魅力.com/a/292379/175093).不要忘记使用您自己的"自定义帖子类型"名称来修改代码.For those who have a problem with the code above, it works like a charm if you replace the second function ( **function na_parse_request()** ) by the one found on this [answer](https://wordpress.stackexchange.com/a/292379/175093). Dont forget to modify the code with your own Custom Post Type name.
- 0
- 2020-04-03
- PhpDoe
-
在WP 5.2出现之前,我一直在使用这个漂亮的代码.更新后,此代码在我的"自定义帖子类型"插件和"高级自定义字段"插件上开始失败,因为我认为它们使用的是相同的pre_get_posts函数,因此我在此插件中看到了我的自定义帖子,而不是"高级自定义字段组"..也因CPT UI插件而失败-无法再创建新帖子,创建后它们也不会出现在列表中.请帮忙!!I have been using this nice code until WP 5.2 came. After the update this code starts to fail on my Custom Post Type plugin and Advanced Custom Fields plugin, because, I think, they are using the same pre_get_posts function, so instead of Advanced Custom Field Groups I see my custom posts in this plugin... Also fails with CPT UI plugin - can not create new posts anymore, they are not appearing in list after creating them. Please help!!
- 0
- 2020-05-04
- Gediminas
-
它适用于单个帖子类型.如何将代码用于多个帖子类型?It worked for single post type. How to use the code for multiple post type?
- 0
- 2020-07-02
- Swaranan Singha Barman
-
- 2017-04-12
将以下代码写入分类法注册.
'rewrite' => [ 'slug' => '/', 'with_front' => false ]
代码更改后必须要做的最重要的事情
更改自定义帖子类型分类法文档后,请尝试转到设置>固定链接并重新保存设置,否则您将获得404页面找到.
Write following code into the taxonomy registration.
'rewrite' => [ 'slug' => '/', 'with_front' => false ]
Most important thing that you have to do after code changing
After you’ve altered your custom post type taxonomy document, try to go to Settings > Permalinks and re-save your settings, else you will get 404 page not found.
-
这实际上有效,我不知道以前没有人注意到这一点.当然,如果它们具有相同的永久链接,则可能会干扰其他页面,但是如果没有,这是一个很好的解决方案.This actually works, i don't know how no one noticed this before. Of course this can interfere with other pages if they have same permalink, but if not this is a great solution.
- 0
- 2017-07-31
- Aleksandar Đorđević
-
试了一下.它为我的自定义帖子类型链接提供了所需的结果.但是,它"捕获"所有POST或PAGE帖子类型的标签,并尝试将它们解析为我的自定义帖子类型的URL,然后是404.(是的,我已经保存了永久链接).Tried this out. It gives the desired result for my custom post type links. However it 'catches' all POST or PAGE post type slugs and tries to resolve them as a URL for my custom post type, then 404s. (yes I've saved permalinks).
- 6
- 2017-10-05
- Matt Keys
-
任何页面和自定义帖子类型都可能有相同的标记,请更改页面标记,然后检查.There might be the same slug of any page and custom post type, change your page slug and then check..
- 0
- 2017-10-11
- Mayank Dudakiya
-
这行不通.即使更新了永久链接也给出404.This doesn't work. Gives 404 even when you've updated permalinks.
- 5
- 2017-11-12
- Christine Cooper
-
@克里斯汀·库珀 您必须遵循此步骤 更改自定义帖子类型分类法文档后,请尝试转到"设置">"固定链接"并重新保存设置,否则将找不到404页.@ChristineCooper You have to follow this step After you’ve altered your custom post type taxonomy document, try to go to Settings > Permalinks and re-save your settings, else you will get 404 page not found.
- 0
- 2017-11-15
- Mayank Dudakiya
-
正如我在上一条评论中所强调的那样,即使您在更新永久链接之后,也仍会收到404错误.请自己尝试一下.As I highlighted in my last comment, you will get an 404 error even *after* you have updated permalinks. Please give it a try yourself.
- 0
- 2017-11-15
- Christine Cooper
-
就像魅力一样工作,尤其是在阅读整个消息时,包括"重新保存设置"部分.+1Works like a charm, especially when reading the whole message, including the "re-save your settings" part. +1
- 0
- 2018-01-25
- davewoodhall
-
同样,即使在重新保存永久链接设置之后,帖子和页面也不再起作用(404)Again, even after re-saving the permalink settings, posts and pages no longer work (404)
- 3
- 2018-02-13
- amklose
-
该解决方案适用于从URL删除该段.但是存档页面不再起作用.This solution works for removing the slug from URL. But the archive pages don't work anymore.
- 1
- 2018-09-25
- Annapurna
-
正如其他人所述,这确实对CPT帖子有效.但这现在导致常规页面的404错误.As others have stated, this does work for the CPT posts themselves. But it's causing a 404 for regular Pages now.
- 0
- 2019-07-03
- Garconis
-
- 2015-09-30
不久前我试图弄清楚这一点,据我所知,简短的回答是否.至少不是在rewrite参数内部.
如果您查看
register_post_type的实际代码,那么详细的解释就会很明显.wp-includes/post.php#L1454"> wp-includes/post.php第1454行: add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
您可以看到它在
$args->rewrite['slug']
重写标签的前面加上%$post_type%
.可能会想到"然后将子弹设置为null
",直到您看到几行:if ( empty( $args->rewrite['slug'] ) ) $args->rewrite['slug'] = $post_type;
您可以看到函数始终期望的值不是空值,否则将使用post类型.
I tried to figure this out not long ago and the short answer from what I know is no. Not from within the rewrite argument at least.
The long explanation becomes apparent if you look at the actual code of
register_post_type
in wp-includes/post.php line 1454:add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
You can see it prefixes
$args->rewrite['slug']
to the%$post_type%
rewrite tag. One could think "let's just set the slug tonull
then" until you look a few lines up:if ( empty( $args->rewrite['slug'] ) ) $args->rewrite['slug'] = $post_type;
You can see that the function always expects a slug value that is not empty and otherwise uses the post type.
-
谢谢@JanBeck.是否存在这个主要原因?为什么不通过有条件地从此规则中忽略某些帖子类型来破解此核心文件?Thanks @JanBeck . Is there a major reason for this to exist? Why not hack this core file with a conditional to omit certain post types from this rule?
- 0
- 2015-09-30
- Ben Racicot
-
您应该将答案授予扬·贝克(Jan Beck).WordPress需要post_type插件来正确路由请求.此规则可防止本机WP页面(不带子弹的呈现)和任何自定义的帖子类型之间的命名冲突.如果您将这些内容破解掉,那么WordPress将不会知道名为"野餐"的页面与名为"野餐"的事件(自定义帖子类型)之间的区别.You should award the answer to Jan Beck. WordPress needs the post_type slug to route requests properly. This rule prevents naming conflicts between native WP pages (which render without the slug) and any custom defined post types. If you hack the slug out then WordPress won't know the difference between a page named "picnic" and an event (custom post type) named "picnic".
- 9
- 2015-09-30
- dswebsme
-
@dswebsme同意,但是在某些情况下您绝对必须更改URL.因此,除了为什么您不能本来也不应这样做之外,您如何有效地做到这一点?@dswebsme Agreed, but there are situations where you absolutely must change the URL. So other than why you can't natively and shouldn't, how do you do so efficiently?
- 3
- 2015-10-01
- Ben Racicot
-
- 2015-10-02
针对我之前的回答: 您当然可以在注册新帖子类型时将
rewrite
参数设置为false
并自己处理重写规则<?php function wpsx203951_custom_init() { $post_type = 'event'; $args = (object) array( 'public' => true, 'label' => 'Events', 'rewrite' => false, // always set this to false 'has_archive' => true ); register_post_type( $post_type, $args ); // these are your actual rewrite arguments $args->rewrite = array( 'slug' => 'calendar' ); // everything what follows is from the register_post_type function if ( is_admin() || '' != get_option( 'permalink_structure' ) ) { if ( ! is_array( $args->rewrite ) ) $args->rewrite = array(); if ( empty( $args->rewrite['slug'] ) ) $args->rewrite['slug'] = $post_type; if ( ! isset( $args->rewrite['with_front'] ) ) $args->rewrite['with_front'] = true; if ( ! isset( $args->rewrite['pages'] ) ) $args->rewrite['pages'] = true; if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) $args->rewrite['feeds'] = (bool) $args->has_archive; if ( ! isset( $args->rewrite['ep_mask'] ) ) { if ( isset( $args->permalink_epmask ) ) $args->rewrite['ep_mask'] = $args->permalink_epmask; else $args->rewrite['ep_mask'] = EP_PERMALINK; } if ( $args->hierarchical ) add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" ); else add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" ); if ( $args->has_archive ) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; if ( $args->rewrite['with_front'] ) $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; else $archive_slug = $wp_rewrite->root . $archive_slug; add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' ); if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) { $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); } if ( $args->rewrite['pages'] ) add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); } $permastruct_args = $args->rewrite; $permastruct_args['feed'] = $permastruct_args['feeds']; add_permastruct( $post_type, "%$post_type%", $permastruct_args ); } } add_action( 'init', 'wpsx203951_custom_init' );
您可以看到
add_permastruct
调用现在不再包含该段. 我测试了两种情况:- 当我使用" calendar"标签创建页面时,该帖子类型存档也会覆盖该页面,该帖子也使用" calendar"模块.
- 当我创建一个带有"my-event"标签的页面和一个带有"my-event"标签的事件(CPT)时,将显示自定义帖子类型.
- 其他任何页面也不起作用.如果您看一下上面的图片,就会清楚为什么:自定义帖子类型规则将始终与页面提示匹配.由于WordPress无法识别是页面还是不存在的自定义帖子类型,因此它将返回404.这就是为什么您需要一个小人物来识别页面或CPT的原因. 一种可能的解决方案是拦截错误并查找可能与该答案类似的页面. li>
In response to my previous answer: you could of course set the
rewrite
parameter tofalse
when registering a new post type and handle the rewrite rules yourself like so<?php function wpsx203951_custom_init() { $post_type = 'event'; $args = (object) array( 'public' => true, 'label' => 'Events', 'rewrite' => false, // always set this to false 'has_archive' => true ); register_post_type( $post_type, $args ); // these are your actual rewrite arguments $args->rewrite = array( 'slug' => 'calendar' ); // everything what follows is from the register_post_type function if ( is_admin() || '' != get_option( 'permalink_structure' ) ) { if ( ! is_array( $args->rewrite ) ) $args->rewrite = array(); if ( empty( $args->rewrite['slug'] ) ) $args->rewrite['slug'] = $post_type; if ( ! isset( $args->rewrite['with_front'] ) ) $args->rewrite['with_front'] = true; if ( ! isset( $args->rewrite['pages'] ) ) $args->rewrite['pages'] = true; if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) $args->rewrite['feeds'] = (bool) $args->has_archive; if ( ! isset( $args->rewrite['ep_mask'] ) ) { if ( isset( $args->permalink_epmask ) ) $args->rewrite['ep_mask'] = $args->permalink_epmask; else $args->rewrite['ep_mask'] = EP_PERMALINK; } if ( $args->hierarchical ) add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" ); else add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" ); if ( $args->has_archive ) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; if ( $args->rewrite['with_front'] ) $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; else $archive_slug = $wp_rewrite->root . $archive_slug; add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' ); if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) { $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); } if ( $args->rewrite['pages'] ) add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); } $permastruct_args = $args->rewrite; $permastruct_args['feed'] = $permastruct_args['feeds']; add_permastruct( $post_type, "%$post_type%", $permastruct_args ); } } add_action( 'init', 'wpsx203951_custom_init' );
You can see the
add_permastruct
call now doesn't include the slug anymore. I tested two scenarios:- When I created a page with the slug "calendar" that page is overwritten by the post type archive which also uses the "calendar" slug.
- When I created a page with the slug "my-event" and an event (CPT) with the slug "my-event", the custom post type is displayed.
- Any other pages do not work either. If you look at the picture above it becomes clear why: the custom post type rule will always match against a page slug. Because WordPress has no way of identifying if it's a page or a custom post type that does not exist, it will return 404. That's why you need a slug to identify either the page or CPT. A possible solution would be to intercept the error and look for a page that might exist similar to this answer.
-
因此,如果目标是删除CPT的代码段,那么我们是否不能为CPT命名一个不会冲突的独特名称,因为它永远不会在URL中显示?或者,如果名称与页面相同,则后名称可能会发生冲突吗?So if the goal is to remove the slug for CPT's couldn't we name the CPT something unique that wouldn't collide since it will never be seen in the URL anyways? Or is the post-name the possible conflict if named the same as a page?
- 0
- 2015-10-02
- Ben Racicot
-
我已经更新了答案,以表明这确实会破坏*所有*页面.如果没有任何提示,WP将查找CPT而不是页面,如果找不到,则返回错误.因此,它实际上与后名无关.I have updated my answer to show that this does actually break *all* pages. Without a slug, WP will look for a CPT instead of a page and if it doesn't find it, return an error. So it's actually not related to the post-name.
- 0
- 2015-10-02
- Jan Beck
-
我懂了.应该有重写规则,将" -1"附加到将来的冲突URL上,例如本机WP帖子与页面.我创建了追踪票https://core.trac.wordpress.org/ticket/34136#ticket会喜欢您的想法.I see. There should be rewrite rules that append '-1' to future conflicting URL's like native WP posts vs pages. I've created a trac ticket https://core.trac.wordpress.org/ticket/34136#ticket would love your thoughts.
- 1
- 2015-10-02
- Ben Racicot
-
- 2019-12-17
插件综述
快到2020年了,其中许多答案都行不通.这是我自己对当前选项的总结:
- Matt Keys答案 似乎是唯一正确的选择您需要自定义代码解决方案.我发现所有插件都无法执行此处列出的所有功能,尤其是重复检查.如果有人想采用这种方法,那么对于插件来说,这似乎是一个非常好的机会.
- Permalink Manager Lite
- 我尝试过的最好的免费插件.
- 完全控制所有Page/Post/CPT完整的永久链接结构,并使它们相同. GUI是迄今为止功能最丰富的.
- 还允许对每个帖子进行完全覆盖,并让您查看原始/默认设置,并在需要时重置为默认设置.
- 支持多站点.
- 不不检查帖子类型之间是否重复,这很可悲.如果页面和CPT具有相同的URL,则页面将加载且CPT无法访问.没有警告或错误,您只需要手动检查重复项即可.
- 所有分类功能均在PRO版本中.升级na很重.
- 自定义永久链接
- 免费版本有很多作用.专业版永久不支持分类学永久链接和高级支持.
- 允许您更改任何单个页面/帖子/CPT的完整永久链接.
- 支持多站点.
- 不允许允许您更改默认结构,因此您的"自定义帖子类型"仍为example.com/cpt-slug/post-title,但您可以单独更改它们.
- 不不检查帖子类型之间是否重复,这很可悲.
- 自定义帖子类型的永久链接
- 允许非开发人员用户使用
register_post_type
更改易于更改的内容
- 不允许允许您更改CPT基本部分-仅更改其后的部分-对于开发人员和此问题中的问题几乎没有用.
- 允许非开发人员用户使用
- 删除基础段... -已经死了好几年了...请不要使用.
Plugin Roundup
It's almost 2020 and a lot of these answers don't work. Here's my own roundup of the current options:
- Matt Keys answer seems to be the only one on the right track if you want a custom code solution. None of the plugins I found can do everything listed here, especially the duplicate checking. This approach seems like a really good opportunity for a plugin if anyone wanted to take that on.
- Permalink Manager Lite
- Best of the free plugins I tried.
- Gives full control over all Page/Post/CPT complete permalink structure and allows them to be the same. The GUI is by far the most feature-rich.
- Allows full override per-post as well and lets you see what the original/default would be and reset to the default if needed.
- Supports multi-site.
- Does not check for duplicates between post types, which is sad. If a page and a CPT have the same URL, the page loads and the CPT is inaccessible. No warnings or errors, you just have to do your own manual checking for duplicates.
- All taxonomy features are in the PRO version. The upgrade nags are pretty heavy.
- Custom Permalinks
- The free version does a lot. Taxonomy permalinks and premium support seem to be the only things withheld from the pro version.
- Allows you to change the full permalink for any individual page/post/CPT.
- Supports multi-site.
- Does not allow you to change the default structure so you your Custom Post Types will still be example.com/cpt-slug/post-title but you can change them individually.
- Does not check for duplicates between post types, which is sad.
- Custom Post Type Permalinks
- Allows non-developer users to change the things that are easy to change already with
register_post_type
- Does not allow you to change the CPT base slug - only the part that comes after that - so pretty much useless for developers and the issue in this question.
- Allows non-developer users to change the things that are easy to change already with
- remove base slug... - dead for several years now... do not use.
-
插件Permalink Manager Lite绝对是最好的解决方案:稳定,强大,干净,免费版本允许您删除子弹底座.它也适用于Polylang!在TwentyTwenty主题下,在Wordpress 5.4上进行了测试,未激活任何其他插件.无论您是否创建了层次结构(带有子帖子和孙子帖子),"自定义帖子类型"都像魅力一样工作.来自每个想要干净解决方案的人.The plugin Permalink Manager Lite is definitively the best solution : steady, robust, clean, and the free version allow you to remove the slug base. And it works with Polylang too ! Tested on Wordpress 5.4, with TwentyTwenty Theme, without any other plugin activated. Works like a charm on Custom Post Type, no matter if you have created a hierarchical one (with child post and grandchild post). From everyone who wants a clean solution.
- 1
- 2020-04-04
- PhpDoe
-
- 2017-02-25
You dont need so much hard-code. Just use lightweight plugin:
It has customizable options.
-
现在,我知道您为什么投票了,它阻止了正常的页面链接解析.我没有看到它,因为尽管刷新了,但仍在缓存现有页面的副本.Now I know why you got downvoted, it prevents normal page links resolving. I didn't see it because I was getting cached copies of the existing pages despite refreshing.
- 0
- 2017-09-14
- Walf
-
@Walf您能详细介绍一下这个问题吗?@Walf Can you tell me about the issue in details?
- 0
- 2017-09-14
- T.Todua
-
从主菜单访问页面链接(不是自定义帖子类型)会产生404错误,就好像该页面不存在一样;而已.Following links to pages (that weren't the custom post type) from the main menu gave 404 errors, as if the page did not exist; that's it.
- 0
- 2017-09-14
- Walf
-
@Walf您能给我您例子的例子网址吗?(如果需要,您可以覆盖域名,我只需要一个示例),谢谢,我将对其进行更新@Walf can you give me any example url of your occasion? (you can cover domain name if you want, i just need an ex example) thanks, i wil update it
- 0
- 2017-09-15
- T.Todua
-
"此插件已于2018年9月19日关闭,无法下载.此关闭是永久的.""This plugin has been closed as of September 19, 2018 and is not available for download. This closure is permanent."
- 1
- 2019-12-16
- squarecandy
-
- 2017-03-01
我们可以对上述功能进行一些更改:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'events', 'page' ) ); } }
至:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { global $wpdb; $pt = $wpdb->get_var( "SELECT post_type FROM `{$wpdb->posts}` " . "WHERE post_name = '{$query->query['name']}'" ); $query->set( 'post_type', $pt ); } }
以设置正确的post_type值.
and we can make some changes to above-mentioned function:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'events', 'page' ) ); } }
to:
function na_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { global $wpdb; $pt = $wpdb->get_var( "SELECT post_type FROM `{$wpdb->posts}` " . "WHERE post_name = '{$query->query['name']}'" ); $query->set( 'post_type', $pt ); } }
in order to set right post_type value.
-
- 2017-05-03
这对我有用:
'rewrite' => array('slug' => '/')
This worked for me:
'rewrite' => array('slug' => '/')
-
这行不通.即使更新了永久链接也给出404.This doesn't work. Gives 404 even when you've updated permalinks.
- 1
- 2017-11-12
- Christine Cooper
-
- 2017-05-29
对于像我这样阅读子帖子时遇到麻烦的任何人,我发现最好的方法是添加自己的重写规则.
我遇到的主要问题是WordPress处理深度为2级(子帖子)的页面的重定向与处理深度为3级(子帖子的孩子)的方式有所不同.
这意味着当我拥有/post-type/post-name/post-child/时,我可以使用/post-name/post-child,它将把我重定向到前面有post-type的那个,但是如果我有post-type/post-name/post-child/post-孙子,那么我不能使用post-name/post-child/post-孙子.
看一看重写规则,它看起来像是在第一层和第二层上与页面名称以外的其他东西匹配(我认为第二层与附件匹配),然后在那里进行一些操作以将您重定向到正确的帖子.在三个层次上,它是行不通的.
您需要做的第一件事就是也从子级中删除帖子类型链接.如果您查看上面的内特·艾伦的答案,那么这种逻辑应该在这里发生:
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
我本人使用各种不同的条件来检查帖子是否有孩子以及诸如此类,以便获得正确的永久链接.这部分并不是很棘手,您会在其他地方找到有人这样做的示例.
尽管如此,下一步是从给定的答案发生变化.与其向主要查询中添加内容(该方法适用于自定义帖子及其子级,而不能用于其他子级),我添加了一个重写,该重写进入了WordPress规则的底部,因此如果pagename不检出并且将要命中404,它会做最后一次检查,以查看自定义帖子类型中的页面是否具有相同的名称,否则它将抛出404.
这是我假设'event'是您的CPT名称的重写规则
function rewrite_rules_for_removing_post_type_slug() { add_rewrite_rule( '(.?.+?)?(:/([0-9]+))?/?$', 'index.php?event=$matches[1]/$matches[2]&post_type=event', 'bottom' ); } add_action('init', 'rewrite_rules_for_removing_post_type_slug', 1, 1);
希望这对其他人有帮助,我找不到与子职位相关的其他东西,并且无法删除其中的子弹.
For anyone reading this that had trouble with child posts like I did I found the best way was to add your own rewrite rules.
The main issue I was having was that WordPress treats the redirect from pages that are 2 levels (child posts) deep a little differently than it treats 3 levels deep (child of child posts).
That means when I have /post-type/post-name/post-child/ I can use /post-name/post-child and it will redirect me to the one with post-type in front but if I have post-type/post-name/post-child/post-grandchild then I can't use post-name/post-child/post-grandchild.
Taking a look into the rewrite rules it looks like it matches for things other than pagename at the first and second levels (I think the second level matches attachment) and then does something there to redirect you to the proper post. At three levels deep it doesn't work.
First thing you need to do is to remove the post type link from children as well. This logic should happen here if you look at Nate Allen's answer above:
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
Myself I used a mix of different conditionals to check if the post had children and whatnot in order to get to the right permalink. This part isn't too tricky and you'll find examples of people doing it elsewhere.
The next step though is where things change from the given answer. Instead of adding things to the main query (which worked for custom posts and their children but not the further children) I added a rewrite that went to the bottom of the WordPress rules so that if pagename didn't check out and it was about to hit a 404 it would do one last check to see if a page within the custom post type had the same name otherwise it would throw out the 404.
Here is the rewrite rule I used assuming 'event' is the name of your CPT
function rewrite_rules_for_removing_post_type_slug() { add_rewrite_rule( '(.?.+?)?(:/([0-9]+))?/?$', 'index.php?event=$matches[1]/$matches[2]&post_type=event', 'bottom' ); } add_action('init', 'rewrite_rules_for_removing_post_type_slug', 1, 1);
Hope this helps someone else, I couldn't find anything else that had to do with child of child posts and removing the slug from those.
-
正则表达式中似乎有一个错字.在'(:'之间需要一个'?'才能将其用作非捕获子模式=>'(?:'.第三个?似乎放错了位置,因为它允许使用空的第一个子模式.应该将其放置在(和:之间.如果没有这种错字,则该表达式将与内置帖子类型"页面"中的表达式相同.There seems to be a typo in the regex. Between '(:' a '?' is needed to use it as non-capturing subpattern => '(?:'. The third ? seems misplaced as it allows an empty first subpattern. Propably it should be positioned between ( and :. Without this typo the expression will be the same as the one which can be found for the build-in post type 'page'.
- 0
- 2019-10-28
- jot
-
- 2019-06-19
这里也遇到了同样的问题,WordPress网站上似乎没有动静.在我的特定情况下,对于单个博客文章,需要/blog/%postname%/结构来解决此问题
https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
以一堆404s结尾
但是,再加上这种绝妙的方法,即没有在博客文章中使用后端永久链接结构,它最终就像魅惑一样工作. https://www.bobz.co/add-blog-prefix-permalink-structure-blog-posts/
谢谢.
Had the same problems here and there seems to be no movement on wordpress site. In my particular situation where for single blogposts the structure /blog/%postname%/ was needed this solution
https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/
ended in a bunch of 404s
But together with this wonderful approach, which is not using the backend permalink strukture for the blogpost it finally works like charme. https://www.bobz.co/add-blog-prefix-permalink-structure-blog-posts/
Thanks a bunch.
-
不鼓励仅链接的答案.如果您在本文中找到的答案与页面上的其他答案不同,请在答案中放入一个摘要和代码示例.Link-only answers are discouraged. If you found an answer in this article that is different from the other answers on the page, please put a summary and code example in your answer.
- 0
- 2019-12-16
- squarecandy
似乎所有网络资源都基于删除自定义帖子类型的子主题,即
现在是非常过时的解决方案,通常引用WP 3.5版之前的安装.常见的做法是:
在register_post_type函数中.这不再起作用,并且具有误导性.所以我问2018年第三季度WordPress 5濒临崩溃的社区...
从rewrite参数或其他任何地方从自定义帖子类型帖子的URL中删除帖子类型提示的现代有效方法是什么?
更新: 似乎有几种方法可以迫使它与正则表达式一起使用.特别是Jan Beck的回答,如果您始终如一地愿意监视内容的创建,以确保不会创建冲突的页面/帖子名称....但是,我坚信这是WP core的主要弱点,应该为我们处理.为永久链接创建CPT时作为选项/挂钩,或作为高级选项集使用.请支持火车票.
脚注:请观看或促销此票,以支持该票: https://core .trac.wordpress.org/ticket/34136#ticket