Purely numerical wordpress permalinks

孤街浪徒 提交于 2019-12-11 00:40:58

问题


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..


回答1:


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.




回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/8007881/purely-numerical-wordpress-permalinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!