我正在尝试向我的网站添加按钮以在社交媒体上共享产品。我已经添加了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>
';
}
有人可以告诉我我在做什么错。谢谢
答案 0 :(得分:2)
在函数中添加全局$ 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>
';
}
答案 1 :(得分:1)
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>
';
}
答案 2 :(得分:0)
为什么不使用WordPress的Easy Social Share Buttons之类的插件
https://codecanyon.net/item/easy-social-share-buttons-for-wordpress/6394476
答案 3 :(得分:0)
您可以使用:
/*
* Facebook, Twitter , Google+ and Pinterest social share buttons For Woocommerce Product
*
*/
function my_social_btn() {
global $post;
// Get the post's URL that will be shared
$post_url = urlencode( esc_url( get_permalink($post->ID) ) );
// Get the post's title
$post_title = urlencode( $post->post_title );
// Compose the share links for Facebook, Twitter and Google+
$facebook_link = sprintf( 'https://www.facebook.com/sharer/sharer.php?u=%1$s', $post_url );
$twitter_link = sprintf( 'https://twitter.com/intent/tweet?text=%2$s&url=%1$s', $post_url, $post_title );
$google_plus_link = sprintf( 'https://plus.google.com/share?url=%1$s', $post_url );
$pinterest_link = sprintf( 'https://pinterest.com/pin/create/bookmarklet/?media=[post-img]&url=%1$s', $post_url );
// Wrap the buttons
$output = '<div id="share-buttons">';
// Add the links inside the wrapper
$output .= '<a target="_blank" href="' . $facebook_link . '" class="share-button facebook"><i class="fa fa-facebook-f"></i></a>';
$output .= '<a target="_blank" href="' . $twitter_link . '" class="share-button twitter"><i class="fa fa-twitter"></i></a>';
$output .= '<a target="_blank" href="' . $google_plus_link . '" class="share-button google-plus"><i class="fa fa-google-plus"></i>+</a>';
$output .= '<a target="_blank" href="' . $pinterest_link . '" class="share-button pinterest"><i class="fa fa-pinterest"></i></a>';
$output .= '</div>';
// Return the buttons and the original content
echo $output;
}
add_action ('woocommerce_after_add_to_cart_form', 'my_social_btn');