我试图添加按钮到我的网站分享一个产品在社交媒体上。我已经添加了PHP代码,但是当您单击facebook时,它会带您到一个带有共享按钮和URL的页面,但是该按钮不会做任何事情。
我需要它看起来像正常的facebook分享界面,你可以添加一个信息,等等。
请看附件中的图片。这个网站是- https://ffe-dev.flowersforeveryone.co.za/product/sunflower-and-rose-bouquet/ (社交媒体按钮在“添加到购物车”按钮下)。
PHP代码是-
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
// add action with variabile in url to share
add_action('woocommerce_after_add_to_cart_button','my_social_btn');
function my_social_btn() {
echo '<div class="my-custom-social">
<div class = "facebook-icon"> <a href="https://www.facebook.com/sharer/sharer.php?u='.$current_url.'" target="_blank" class="social fb"><i class="fa fa-facebook-f"></i></a> </div>
<div class = "twitter-icon"> <a href="https://twitter.com/intent/tweet?url='.$current_url.'" target="_blank" class="social tw"><i class="fa fa-twitter"></i></a> </div>
<div class = "pinterest-icon"><a href="
https://pinterest.com/pin/create/bookmarklet/?media=[post-img]&url='.$current_url.'" target="_blank" class="social tw"><i class="fa fa-pinterest"></i></a> </div>
</div>
';
}
有人能告诉我我做错了什么吗。谢谢
发布于 2019-08-13 07:20:08
在函数中添加全局$wp和$current_url。由于它是外部函数,所以您无法获得$current_url的价值来共享文章。
// add action with variabile in url to share
add_action('woocommerce_after_add_to_cart_button', 'my_social_btn');
function my_social_btn() {
global $wp;
$current_url = home_url(add_query_arg(array(), $wp->request));
echo '<div class="my-custom-social">
<div class = "facebook-icon"> <a href="https://www.facebook.com/sharer/?u=' . $current_url . '" target="_blank" class="social fb"><i class="fa fa-facebook-f"></i></a> </div>
<div class = "twitter-icon"> <a href="https://twitter.com/intent/tweet?url=' . $current_url . '" target="_blank" class="social tw"><i class="fa fa-twitter"></i></a> </div>
<div class = "pinterest-icon"><a href="
https://pinterest.com/pin/create/bookmarklet/?media=[post-img]&url=' . $current_url . '" target="_blank" class="social tw"><i class="fa fa-pinterest"></i></a> </div>
</div>
';
}
发布于 2019-08-13 07:17:04
为什么不为WordPress使用像Easy共享按钮这样的插件呢?
https://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476
发布于 2019-08-13 07:21:47
add_action('woocommerce_after_add_to_cart_button','my_social_btn');
function my_social_btn() {
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
echo '<div class="my-custom-social">
<div class = "facebook-icon"> <a
href="https://www.facebook.com/sharer/sharer.php?u='.$current_url.'"
target="_blank" class="social fb"><i class="fa fa-facebook-f"></i>hjgj</a>
</div>
<div class = "twitter-icon"> <a href="https://twitter.com/intent/tweet?url='.$current_url.'" target="_blank" class="social tw"><i class="fa fa-twitter">
</i>ghjg</a> </div>
<div class = "pinterest-icon"><a href="https://pinterest.com/pin/create/bookmarklet/?media=[post-img]&url='.$current_url.'" target="_blank" class="social tw"><i class="fa fa-pinterest"></i>ghj</a> </div>
</div>
';
}
https://stackoverflow.com/questions/57472588
复制相似问题