自动获取wordpress帖子的短URL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动获取wordpress帖子的短URL相关的知识,希望对你有一定的参考价值。

this is how to automatically get short urls for your blog posts in wordpress,

What you need
is to decide which tinyurl service you will be using and get its api URL,
for example rinyurl.com , bit,ly, tr.im,

here are the api URLS for the most famous services.

tinyurl.com : http://tinyurl.com/api-create.php?url=
is.gd : http://is.gd/api.php?longurl=
href.in : http://href.in/api.php?create=
tr.im : http://api.tr.im/api/trim_simple?url=

Note: There are some short url services api's which requires authentication, like bit.ly , so keep that in mind, but my above services are ok!
  1. /* add the following codes to functions.php file: */
  2.  
  3. function getShortUrl($url) {
  4. $tinyurl = file_get_contents("API-URL".$url);
  5. return $tinyurl;
  6. }
  7.  
  8. // remember to replace API-URL in the second line of the above codes with one of the urls mentioned on the comments
  9.  
  10. /* On your single.php file, paste the following within the loop: */
  11. <?php
  12. $surl = getShortUrl(get_permalink($post->ID));
  13. echo 'Short Url for this post: <a href="'.$surl.'">'.$surl.'</a>'
  14. ?>

以上是关于自动获取wordpress帖子的短URL的主要内容,如果未能解决你的问题,请参考以下文章

如何从 wordpress 中的帖子页面 id 获取 acf 的画廊图片 url

php 获取Wordpress中主要帖子页面的URL

PHP 自动获取wordpress帖子的简短网址

PHP Wordpress查询从帖子/页面中提取片段

Wordpress 5.2 YouTube 视频自动嵌入帖子不起作用

Wordpress - 短 mp4 视频而不是特色图片