Permalinks setting

admin2025-06-05  3

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 are example/categoryname/postname
  • Posts: /thema/%category%/%postname%/ + Category: blog/thema = categories work, posts are example/thema/categoryname/postname
  • Posts: /blog/thema/%category%/%postname%/ + Category: thema = posts work as example/blog/thema/categoryname/postname/ but categories work like example/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 are example/categoryname/postname
  • Posts: /thema/%category%/%postname%/ + Category: blog/thema = categories work, posts are example/thema/categoryname/postname
  • Posts: /blog/thema/%category%/%postname%/ + Category: thema = posts work as example/blog/thema/categoryname/postname/ but categories work like example/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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

It'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/
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749087328a316254.html

最新回复(0)