WordPress中的随机永久链接键 [英] Random permalink key in wordpress

查看:109
本文介绍了WordPress中的随机永久链接键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为WordPress中的每个新帖子提供一个自定义的永久链接,例如: http://mysite.com/x5Kvy6(就像bit.ly).

I want to have a custom permalink for each new post in WordPress like: http://mysite.com/x5Kvy6 (like bit.ly).

我尝试了这个小脚本,但是它在永久链接中仅将5位数字添加到帖子标题中.

I tried this little script, but it adds only 5-digit numbers to the post title in the permalink.

function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {

if($slug!=""){
  $random=rand(11111,99999); //I needed 5 digit random
  $slug .= "-" . $random;
}
return $slug;

}

如何制作随机密钥而不是帖子标题?

How can I make a random key instead of the post title?

我还没有研究URL缩短器或重定向方法.

I have not researched URL shorteners or redirection methods.

欢迎提出任何想法!

推荐答案

function wp_unique_post_slug($col,$table='wp_posts'){
     global $wpdb;

     $alphabet = array_merge( range(0, 9), range('a','z') );

     $already_exists = true;
     do {

         $guidchr = array();
         for ($i=0; $i<32; $i++)
         $guidchr[] = $alphabet[array_rand( $alphabet )];


         $guid = sprintf( "%s", implode("", array_slice($guidchr, 0, 12, true)) );

       // check that GUID is unique
       $already_exists = (boolean) $wpdb->get_var("
       SELECT COUNT($col) as the_amount FROM $table WHERE $col = '$guid'
       ");

      } while (true == $already_exists);

     return $guid;
}

可以通过多种方式对其进行优化.

This can be optimised in a number of ways.

关于此wp_unique_post_slug()-yikes还要注意名称间隔. WordPress已使用此功能名称

Also regarding this wp_unique_post_slug() -- yikes watch out for the name spacing. Wordpress already uses this function name

这篇关于WordPress中的随机永久链接键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆