My WordPress website currently uses this custom permalink structure:
%author%/%year%/%monthnum%/%day%/%postname%
In this case, %monthnum% instantiates numbered dates with a leading zero — e.g., “09” for September. So, a sample URL might look like this:
mywebsite.com/username/2012/09/12/post-name
Is there a function I can add or .htaccess change that I can make which will remove the leading zeros from my permalink stucture? So, using the example above, my URLs would ideally look like this:
mywebsite.com/username/2012/9/12/post-name
Thank you! I’ve read up on WordPress’ structure tags documentation (http://codex.wordpress.org/Using_Permalinks), but I can’t find any solutions or plugins for the above-mentioned problem.
How about using custom rewrite tags/structure?
So we’ll be using these two rewrite/structure tags:
%monthnum2%
does the same thing as the%monthnum%
tag, but without a leading zero; e.g.3
and not03
for March.%day2%
does the same thing as the%day%
tag, but without a leading zero; e.g.7
and not07
.The steps:
In the theme functions file (
functions.php
), add:That will generate the
%monthnum2%
and%day2%
(rewrite) tags and be used when WordPress (re-)generates the rewrite rules.And then add this:
That will replace the rewrite tags in the permalink.
Go to the permalink settings page and then in the “Custom Structure” box, enter this structure:
/%year%/%monthnum2%/%day2%/%postname%/
or/%author%/%year%/%monthnum2%/%day2%/%postname%
, whichever applies.The point is, use
%monthnum2%
to display the month number without a leading zero and%day2%
to display the day number without a leading zero.Save your changes — both the theme functions file and the permalink settings — and go to the “Posts” page (
wp-admin/edit.php
) and just check everything (mouse-over the post permalink and visit the post).Filter
'month_link'
and'day_link'
, WordPress will find the matching posts then without further work.Sample code:
htaccess solution
I created a working example for you to test here
What this does is check for the path structure provided if the third path contains, a leading 0 and number 1-9, eg /03/, then applies the rewrite rule to the third-path, leaving the first two paths and additional paths intact
(.*?)/(.*?)/
. The rule only changes the third path if that path matches (01-09)[0]([1-9])
from eg /03/ to /3/.Does not affect /uploads/ month images path because the uploads structure does not match the structure provided. Please test using above link to see if it suits your needs.