从“自定义帖子类型”中删除“ Slug”,结果为404
3 个回答
- 投票数
-
- 2018-01-27
自定义帖子类型和永久链接修改的注册可以.问题在于WordPress重写规则很可能与您的页面简单链接的"清理" URL相匹配,并且将设置
pagename
查询变量而不是name
假设您的change_slug_struct()
函数.因此将功能更改为此,以解决所有情况:
function change_slug_struct( $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', 'single-link', 'page' ) ); } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) { $query->set( 'post_type', array( 'post', 'single-link', 'page' ) ); // We also need to set the name query var since redirect_guess_404_permalink() relies on it. $query->set( 'name', $query->query['pagename'] ); } } add_action( 'pre_get_posts', 'change_slug_struct' );
The registering of the custom post type and the permalink modification is OK. The problem is with the WordPress rewrite rules that more than likely will match the "cleaned up" URL of your simple links to pages and it will set the
pagename
query var notname
as yourchange_slug_struct()
function assumed.So change the function to this to account for all cases:
function change_slug_struct( $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', 'single-link', 'page' ) ); } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) { $query->set( 'post_type', array( 'post', 'single-link', 'page' ) ); // We also need to set the name query var since redirect_guess_404_permalink() relies on it. $query->set( 'name', $query->query['pagename'] ); } } add_action( 'pre_get_posts', 'change_slug_struct' );
-
有了帮助,我什至不需要更改或刷新固定链接.谢谢!!that helped, I didn't even need to change or refresh the permalinks. Thank you!!
- 0
- 2018-01-29
- Paranoia
-
如果我有一个带有父页面的页面(例如domain.com/parent/page),那我得到的是404.如果我将其更改为domain.com/page(没有父页面),那么它将再次起作用.知道这可能是什么吗?that didn't fully work, if I have a page with a parent (eg. domain.com/parent/page) i get a 404. If I change it to domain.com/page (without the parent) then it works again. Any idea what this could be?
- 0
- 2018-01-29
- Paranoia
-
如果有解决方案,我会改回正确的答案.不幸的是我被困住了:(I'll change this back to the correct answer, if we have a solution. Unfortunately I am stuck :(
- 0
- 2018-01-30
- Paranoia
-
是.我没有考虑到存在子页面的情况.由于您的自定义帖子类型不是分层的,因此可以安全地排除存在子页面的情况.我已经修改了答案.请让我知道它是否有效,并且不要忘记将其作为正确的答案推向市场.Yes. I haven't taken into account the case where there are child pages. Since your custom post type is not hierarchical, it is safe to exclude cases when there are child pages. I have modified my answer. Do let me know if it works and don't forget to market it as the right answer.
- 0
- 2018-01-30
- Vlad Olaru
-
你是个天才!you're a genius!!
- 0
- 2018-01-30
- Paranoia
-
- 2018-01-23
您必须更改永久结构.默认情况下,只有在网址以Slug前缀开头的情况下,才能找到您自定义帖子类型的帖子.更改前缀时,您会遇到与删除前缀时类似的问题,查看此帖子.
要删除帖子类型的前缀,您应该插入
single-link_rewrite_rules
并遍历这些规则,并在那里删除前缀.注意:永久结构的更改可能会导致网址冲突.
You have to alter the perma structure. By default your custom post type's post will only be found wenether the url starts with the slug prefix. When changing the prefix you will have similar issues as when deleting it, have a look at this post.
To achieve removal of the post type slug prefix you should hook into
single-link_rewrite_rules
and iterate through those rules and remove the prefix there as well.Note: changes in the perma structure may cause url conflicts.
-
@Paranoia不能100%确定"使用页面类型"的含义.但是使用[`rewrite_rules_array`](https://codex.wordpress.org/Plugin_API/Filter_Reference/rewrite_rules_array)钩子,您可以访问所有规则.@Paranoia not 100% sure what you mean with "use the page type". But with the [`rewrite_rules_array`](https://codex.wordpress.org/Plugin_API/Filter_Reference/rewrite_rules_array) hook you can access all rules.
- 0
- 2018-01-24
- Fleuv
-
- 2020-07-22
对于多种自定义帖子类型,请按以下步骤进行调整
$query->set( 'post_type', array( 'post', 'custom1', 'page' ) && array( 'post', 'custom2', 'page' ) );
For multiple Custom Post Types adjust like this
$query->set( 'post_type', array( 'post', 'custom1', 'page' ) && array( 'post', 'custom2', 'page' ) );
我正在使用一个创建列表的插件.创建列表后,我想从网址中删除该子弹
帖子类型:
从网址中删除该标签:
(此代码来自此处)
现在,点击"发布"后,该子弹/single-link/就会被删除,但是访问该页面时我们总是会收到404.更改/重新保存永久链接没有帮助.我在做什么错了?