• Hi,
    I would like to only use the top level category in the permalinks of my posts.

    For example, lets say I have this category structure:

    -Recipes
    –Appetizer
    –Main dish
    –Lunch
    –Soup
    –Dessert
    –Vegetarian
    –Etc…

    Now let’s say I create a post for “Garden vegetable soup recipe”, I would tag this post with the 3 following categories “appetizer” , “soup” and “vegetarian”.

    If I set my permalink structure to /%category%/%postname%/ this result in a url like :
    https://domain.com/recipes/appetizer/garden-vegetable-soup

    This is not what I would want, instead I would like :
    https://domain.com/recipes/garden-vegetable-soup

    I do not want to have any sub-categories in the url (most posts are associated with multiple sub-categories).

    I tried the plugin Single category permalink but it didnt work for me, not sure why… ( tried to add the top level category to each post, but the urls kept including the subcategory even after I reset the permalink structure in wordpress)

    NOTE: you might wonder why I have the “recipes” top level category at all if I do not tag any post with it, this is because I also have other top level categories for other topics, similarly for those other topics, I want their own respective top level categories to be the only category appearing in the permalinks…

    Thanks,

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It would take a little custom coding to alter the permalink WP uses. You would hook the ‘post_link_category’ filter, in which you can return the parent category’s object instead of the child category object WP would normally use. Since the rest of the permalink function now thinks this parent is the assigned category, it will not insert any child category into the permalink.

    If the category passed to your callback has no parent, it’s already a top level category, so your callback would then just return the object it was passed.

Viewing 1 replies (of 1 total)
  • The topic ‘How to only use top category in permalinks’ is closed to new replies.