如何在条件标签中回显时在 URL 中包含 wordpress 标题


How do I include the wordpress title in the URL while echo in conditional tag?

以下是显示WhatsApp共享图标的代码,条件wp_is_mobile

<?php
  if (wp_is_mobile()) {
  echo '<a href="whatsapp://send?text=' . the_title() . ', from ' . get_bloginfo('name') . '   ' . wp_get_shortlink() . '" data-action="share/whatsapp/share">';
  echo '<i class="fa fa-whatsapp"></i></a>';
}
 ?>

the_title在屏幕上的WhatsApp图标之前显示帖子标题,而不是在URL中。如何使帖子标题成为URL的一部分,而不是显示在屏幕上的文本中

你需要使用get_the_title(),但除此之外,我会urlencode()整个事情:

<?php
    if (wp_is_mobile()) {
        echo '<a href="whatsapp://send?text=' . urlencode(get_the_title() . ', from ' . get_bloginfo('name') . '   ' . wp_get_shortlink()) . '" data-action="share/whatsapp/share">';
        echo '<i class="fa fa-whatsapp"></i></a>';
    }
?>

如下所述:

(the_title()) 显示或返回当前帖子的标题。此标签只能在循环中使用,以获取循环使用get_the_title之外的帖子标题。如果帖子是受保护的或私有的,则会在标题前面加上"受保护:"或"私人:"字样来注明。