Have you ever seen sites that have a little input field at the bottom of their post with a short link to their article? Well usually that shortlink is coming from the WordPress get_shortlink filter. In this article, we will show you how you can use the get_shortlink codes to display a little input box in your post. It will also allow your user to select the shortlink on one click which makes it easy for them to copy and paste it.
您是否曾经见过在其帖子底部有一个小输入字段并带有指向其文章的简短链接的网站? 通常,短链接来自WordPress的get_shortlink过滤器。 在本文中,我们将向您展示如何使用get_shortlink代码在帖子中显示一个小的输入框。 它还允许您的用户单击一次选择短链接,这使他们可以轻松复制和粘贴它。
All you have to do is put this code somewhere in the loop (usually in single.php) file.
您所要做的就是将此代码放在循环文件中的某个位置(通常在single.php中)。
<?php if (function_exists('wp_get_shortlink')) { ?>
<div><span class="post-shortlink">Shortlink:
<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' />
</span></div>
<?php } ?>
This will bring you the same shortlink that you see when you click the Get Shortlink Button below your post title.
这将为您带来与单击帖子标题下方的“获取短链接”按钮时看到的相同的短链接。
![Get Shortlinks in WordPress Theme](https://i-blog.csdnimg.cn/blog_migrate/ecfb451b07d4849fce352ca0a3c9083b.png)
By default it is yoursite.com/?p=1082 or something similar. But that can be replaced with other services by using any shortlink plugin that uses the “get_shortlink” filter to override the shortlink properly like WordPress.com Stats for (wp.me) or WP Bitly for (Bit.ly or Bit.ly PRO)
默认情况下为yoursite.com/?p=1082或类似名称。 但这可以通过使用任何使用“ get_shortlink”过滤器的短链接插件来替代其他服务来正确覆盖该短链接,例如(wp.me)的WordPress.com统计信息 (Bit.ly或Bit.ly PRO)的WP Bitly 。
Once you have the code added in your theme, the preview would look like this:
在主题中添加代码后,预览将如下所示:
![Code Preview in WordPress Theme](https://i-blog.csdnimg.cn/blog_migrate/cf8dca07ea92d060aa9e19f785084c92.png)
Source: OttoPress
资料来源: OttoPress
翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-wordpress-shortlinks-in-your-theme/