Purely numerical wordpress permalinks

2019-07-21 04:45发布

Can I have single digit wordpress permalinks? Trying to force the permalink to "2" e.g. automatically rewrites it to "2-2".

Looking into it a bit further I discovered this is the case for any numerical permalink - I reckon it has to do with possible interference with paging but have a use case where I would like to use this structure..

3条回答
Anthone
2楼-- · 2019-07-21 05:15

You probably already have a post with the slug '2'; if it's not in the post administration panel, it might still be in the trash.

On a side note, using digits as slugs might not be the best idea--it could conflict with paging and other rewrite rules, and might not describe what that post is for.

Hope this helps!

查看更多
放我归山
3楼-- · 2019-07-21 05:16

actually this can be easily done from the permalinks section in the settings. You can simply set /post_id/ as the permalink structure and it will work fine.

查看更多
在下西门庆
4楼-- · 2019-07-21 05:39

I've dealt with this once by intercepting the post slug creation and "fixing" it somehow. Maybe you can work from this:

 /*
 * New permalink structure
 * 
 * Itercepts every wp_insert_post data to change the regular slugs to md5 hashes 
 * based on the Unix timestamp of action. This generates a new slug at every 
 * saving action, which protects the posts against unauthorised direct access.
 * 
 */
function rm_cpt_permalink_change($data) {

    if ($data['post_type'] == 'custom_post_type') {

                $time = time();
        $data['post_name'] = md5($time);
        return $data;
    } else {
            return $data;
        }
}
add_filter('wp_insert_post_data','rm_cpt_permalink_change',10,2);

Do you really need single-digit slugs? If you can have your way with 00001 or 000005, the wp_insert_post_data filter is probably the way to go.

查看更多
登录 后发表回答