admin管理员组文章数量:1130349
so, I know this is kind of a basic question, but it somehow oddly doesn't work for me.
What I want:
- /categoryname/postname (for posts)
- /categoryname (for categories)
Here are my settings:
Sadly that doesn't work. Here is a detailed information on what works on what settings:
- Posts:
/blog/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts getting 404 - Posts:
/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/categoryname/postname - Posts:
/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/thema/categoryname/postname - Posts:
/blog/thema/%category%/%postname%/+ Category:thema= posts work asexample/blog/thema/categoryname/postname/but categories work likeexample/thema/categoryname
Maybe someone can help me. I won't mind if this is only solvable with some htaccess magic or whatever. :)
so, I know this is kind of a basic question, but it somehow oddly doesn't work for me.
What I want:
- https://example/blog/thema/categoryname/postname (for posts)
- https://example/blog/thema/categoryname (for categories)
Here are my settings:
Sadly that doesn't work. Here is a detailed information on what works on what settings:
- Posts:
/blog/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts getting 404 - Posts:
/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/categoryname/postname - Posts:
/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/thema/categoryname/postname - Posts:
/blog/thema/%category%/%postname%/+ Category:thema= posts work asexample/blog/thema/categoryname/postname/but categories work likeexample/thema/categoryname
Maybe someone can help me. I won't mind if this is only solvable with some htaccess magic or whatever. :)
Share Improve this question asked Dec 18, 2018 at 14:14 marvinpoomarvinpoo 3033 silver badges18 bronze badges2 Answers
Reset to default 1It's possible to have them share the same slug, but you need to manually resolve conflicts yourself. You have to check if the end of the requested URL is an existing term, and reset the query vars accordingly if it's not found:
function wpd_post_request_filter( $request ){
if( array_key_exists( 'category_name' , $request )
&& ! get_term_by( 'slug', basename( $request['category_name'] ), 'category' ) ){
$request['name'] = basename( $request['category_name'] );
$request['post_type'] = 'post';
unset( $request['category_name'] );
}
return $request;
}
add_filter( 'request', 'wpd_post_request_filter' );
The obvious downsides to this are an extra trip to the database for every post or category view, and the inability to have a category slug that matches a post slug.
The problem with what you're trying to do is that with your desired structure it's impossible to tell if a URL is for a subcategory or a post.
Take this URL, for example:
http://example/blog/thema/abc/def/
Is def a post in the abc category? Or is def a subcategory of abc? I can't tell by looking at the URL, and neither can WordPress. The issue is that WordPress isn't going to try both options.
WordPress is only ever going to look for one type of content when parsing the URL. So when WordPress sees that URL it checks if there's a subcategory of abc called def, and if that category doesn't exist WordPress will return a 404. This is why you're getting a 404 when attempting to view a post.
You're going to need to add something to the structure to differentiate a post from a subcategory. For example, if you used /blog/post/%category%/%postname% as the structure, then the above URL would look like this for a subcategory:
http://example/blog/thema/abc/def/
And this for a post:
http://example/blog/post/abc/def/
so, I know this is kind of a basic question, but it somehow oddly doesn't work for me.
What I want:
- /categoryname/postname (for posts)
- /categoryname (for categories)
Here are my settings:
Sadly that doesn't work. Here is a detailed information on what works on what settings:
- Posts:
/blog/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts getting 404 - Posts:
/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/categoryname/postname - Posts:
/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/thema/categoryname/postname - Posts:
/blog/thema/%category%/%postname%/+ Category:thema= posts work asexample/blog/thema/categoryname/postname/but categories work likeexample/thema/categoryname
Maybe someone can help me. I won't mind if this is only solvable with some htaccess magic or whatever. :)
so, I know this is kind of a basic question, but it somehow oddly doesn't work for me.
What I want:
- https://example/blog/thema/categoryname/postname (for posts)
- https://example/blog/thema/categoryname (for categories)
Here are my settings:
Sadly that doesn't work. Here is a detailed information on what works on what settings:
- Posts:
/blog/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts getting 404 - Posts:
/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/categoryname/postname - Posts:
/thema/%category%/%postname%/+ Category:blog/thema= categories work, posts areexample/thema/categoryname/postname - Posts:
/blog/thema/%category%/%postname%/+ Category:thema= posts work asexample/blog/thema/categoryname/postname/but categories work likeexample/thema/categoryname
Maybe someone can help me. I won't mind if this is only solvable with some htaccess magic or whatever. :)
Share Improve this question asked Dec 18, 2018 at 14:14 marvinpoomarvinpoo 3033 silver badges18 bronze badges2 Answers
Reset to default 1It's possible to have them share the same slug, but you need to manually resolve conflicts yourself. You have to check if the end of the requested URL is an existing term, and reset the query vars accordingly if it's not found:
function wpd_post_request_filter( $request ){
if( array_key_exists( 'category_name' , $request )
&& ! get_term_by( 'slug', basename( $request['category_name'] ), 'category' ) ){
$request['name'] = basename( $request['category_name'] );
$request['post_type'] = 'post';
unset( $request['category_name'] );
}
return $request;
}
add_filter( 'request', 'wpd_post_request_filter' );
The obvious downsides to this are an extra trip to the database for every post or category view, and the inability to have a category slug that matches a post slug.
The problem with what you're trying to do is that with your desired structure it's impossible to tell if a URL is for a subcategory or a post.
Take this URL, for example:
http://example/blog/thema/abc/def/
Is def a post in the abc category? Or is def a subcategory of abc? I can't tell by looking at the URL, and neither can WordPress. The issue is that WordPress isn't going to try both options.
WordPress is only ever going to look for one type of content when parsing the URL. So when WordPress sees that URL it checks if there's a subcategory of abc called def, and if that category doesn't exist WordPress will return a 404. This is why you're getting a 404 when attempting to view a post.
You're going to need to add something to the structure to differentiate a post from a subcategory. For example, if you used /blog/post/%category%/%postname% as the structure, then the above URL would look like this for a subcategory:
http://example/blog/thema/abc/def/
And this for a post:
http://example/blog/post/abc/def/
本文标签: Permalinks setting
版权声明:本文标题:Permalinks setting 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749087284a2313971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论