Prefix blog posts but not for tag and category archive page
TLDR; Install https://github.com/jmslbam/wp-post-url-prefixer to automaticly prefix your posts with blog
and also create a /blog/
post archive page.
Prefix posts via Permalinks settings is not good enough
So for XvM Coaching I wanted to add a prefix to the blog posts and did so via the WordPress Permalink settings. But this also added the prefix in front of the tag and category urls. So those would because /blog/category/hsp/
or /blog/tags/hechtingsstijlen/
. But I didn’t want that, because one could register the default category
Taxonomy to a new Custom PostType and then the /blog
prefix would not be logical.
I googled if there would be a little plugin available, but only stubled on some Stack Overflow questions without any accepted anwsers. But I did use the snippets and combined them into a small plugin.
Install the plugin and configure your prefix via a filter which defaults to blog
as prefix. So if that’s what you need 👌 and just activate it. Otherwise try this to overwrite the prefix value:
add_filter('wp_pup_singular_prefix', function(){
return 'news';
});
Automaticly enabled blog archive
And by providing a prefix for posts we can also enable a blog archive, because by default this is disabled for the core post
post type.
The plugin enables the archive to /blog/ so you don’t need to create a page and set via ‘Reading settings’. We also add this new Post Archive page to the Yoast Breadcrumb. If you do use that option, the plugin won’t do anything.
If you want to overwrite the slug
of the posts archive then use this snippet:
add_filter('wp_pup_archive_slug', function(){
return 'news';
});
To disable this automaticly created post archive, use this snippet:
add_filter('wp_pup_archive_slug', function(){
return false;
});
Beware that if you create a page with a similiar slug
as the prefix, the archive.php
template with be loaded and not your home.php
page a.k.a. “Posts page” a.k.a. is_home()
.
Have fun!