我希望能从一些使我发疯的编码问题中获得帮助。我最好在wordpress帖子标题中写“&”而不是“and”。但是写出“&”号会破坏我们的Twitter,Facebook和Google+帖子共享链接。 Facebook能够实际显示链接(尽管标题中的符号是“&”号),但是完整的Twitter失败了,google-plus也是如此。

这是共享链接的代码:

<ul>
    <li class="video-twitter"><a target="_blank" href="http://twitter.com/share?text=<?php the_title(); ?>&amp;url=<?php the_permalink(); ?>" title="Share on Twitter">Twitter</a></li>
    <li class="video-facebook"><a target="_blank" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Facebook">Facebook</a></li>
    <li class="video-google"><a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Google+">Google+</a></li>
</ul>

任何帮助将不胜感激!

最佳答案

我今天遇到了同样的问题,很容易解决。 WordPress将所有&符等作为实体,这意味着如果您使用get_the_title()或the_title()&符,则如下所示:&#038;

您可以使用html_entity_decode()对此进行解码,然后必须使其对URL友好,这可以使用urlencode()完成。

合并它们,您将获得:

<?php print urlencode( html_entity_decode( get_the_title() ) ); ?>

或者以“更清洁”的方式进行操作,然后在您的functions.php主题文件中创建此函数:
function themeprefix_social_title( $title ) {
    $title = html_entity_decode( $title );
    $title = urlencode( $title );
    return $title;
}

这样,您可以在主题中调用此功能,该功能可用于所有社交网络:
<?php print themeprefix_social_title( get_the_title() ); ?>

当应用于您的示例时:
<ul>
    <li class="video-twitter"><a target="_blank" href="http://twitter.com/share?text=<?php print themeprefix_social_title( get_the_title() ); ?>&url=<?php the_permalink(); ?>" title="Share on Twitter">Twitter</a></li>
    <li class="video-facebook"><a target="_blank" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php print themeprefix_social_title( get_the_title() ); ?>" title="Share on Facebook">Facebook</a></li>
    <li class="video-google"><a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink();?>&t=<?php print themeprefix_social_title( get_the_title() ); ?>" title="Share on Google+">Google+</a></li>
</ul>

关于wordpress - WordPress标题中的“&”号打破了我与社交媒体链接的份额,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20464311/

10-13 00:29