WordPress标题中的“&"号打破了我与社交媒体链接的份额 [英] Ampersands in Wordpress titles break my share to social media links

查看:84
本文介绍了WordPress标题中的“&"号打破了我与社交媒体链接的份额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was hoping to get some help with a bit of a coding problem that has been driving me crazy. I'd preferably like to write "&" instead of "and" in my wordpress post titles. But writing out ampersands breaks our post share links for twitter, facebook, and google-plus. Facebook is able to actually display the link (it takes the ampersand out of the title, though), but twitter full out fails, and google-plus as well.

这是共享链接的代码:

<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;

I had the same problem today and it's rather easy to fix. All ampersands and such are served as entities by WordPress, this means if you use get_the_title() or the_title() ampersands are like this: & #038 ;

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

You can decode this using html_entity_decode() after that you'll have to make it URL friendly, which can be done using urlencode().

合并它们,您将获得以下内容:

Combine them and you'll have this:

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

或者以更清洁"的方式进行操作,并在您的functions.php主题文件中创建此函数:

Or do it the 'cleaner' way and create this function in your functions.php theme file:

function themeprefix_social_title( $title ) {
    $title = html_entity_decode( $title );
    $title = urlencode( $title );
    return $title;
}

这样,您可以在主题中调用此功能,该功能可用于所有社交网络:

This way you can call this function in your theme, which can be used for all social networks:

<?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标题中的“&"号打破了我与社交媒体链接的份额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆