如何在主题中显示WordPress短链接

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

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

Source: OttoPress

资料来源: OttoPress

翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-wordpress-shortlinks-in-your-theme/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值