How to Customize WordPress Site Permalink

WordPress is by far the most popular blogging CMS. Here in Technig, we have quite a few tips and tricks about WordPress and theme development. In this short tutorial, you will learn how to customize WordPress site permalink to make it look pretty.

How to Change WordPress Site Permalink?

WordPress makes it easy to change and customize site permalink or url in just a few options. Open your WordPress dashboard and go to settings > permalinks.

Customize WordPress site permalink

By default, there quite a few options that you can choose.

Customize WordPress site permalink

But, sometime this options are not what you want. Fore instance, you might want to show the post-title/id or id/post-title.

Example:

https://example.com/I-love-coding/55123

In those situations, the default options would’t work.

How to Customize WordPress Site Permalink

Basically, There are two ways to customize and make the permalink pretty. One, by writing a simple function in your functions.php file of you theme. Two, writing a custom structure to the given option in permalinks page.

We will go through but ways. The only disadvantage for first way is this. If you change your theme, the options might not work. So make sure which you way is suitable for you. 

First Option

Open your functions.php file which is in your active theme directory and past the following code.

add_action( "init", "technig_permalink" );
function technig_permalink() {

    //This rule will match : I-love-coding/55123
    add_rewrite_rule(        
        '^([^/]*)/([0-9]+)/?',        
        'index.php?p=$matches[2]',        
        'top' );

   //This rule will match : 55123/I-love-coding
    add_rewrite_rule(        
        '^([0-9]+)/([^/]*)/?',        
        'index.php?p=$matches[1]',        
        'top' );

}

Choose one the above rules.

Second Option

The second option will be easy and most convenient way. Select the Custom Structure and write the following value.

/%postname%/%post_id%
Customize WordPress URL

That’s it. There are many other options and variable that you might want to use.

 %year%
The year of the post, four digits, for example 2004
 %monthnum%
Month of the year, for example 05
 %day%
Day of the month, for example 28
 %hour%
Hour of the day, for example 15
 %minute%
Minute of the hour, for example 43
 %second%
Second of the minute, for example 33
 %post_id%
The unique ID # of the post, for example 423
 %postname%
A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So “This Is A Great Post!” becomes this-is-a-great-post in the URI.
 %category%
A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI.
 %author%
A sanitized version of the author name.

Conclusion

WordPress is not only a blogging CMS, you can customize every  piece of it and make it look the way you want. We hope you have learned something and if you have any questions, feel free to comment it bellow. 🙂

 

How toWordPress TipsWordPress Tutorials
Comments (0)
Add Comment