如何从Wordpress中的单页删除自定义共享按钮

时间:2017-07-20 03:45:44

标签: php wordpress

我刚在WordPress中添加了分享按钮。我想在帖子上显示它,但它也在Pages上显示。如何从Pages中删除按钮。这是我的网站链接http://mycodeskill.com

我使用了此代码

    function mcs_social_sharing_buttons($content) {
    global $post;
    if(is_singular() || is_home()){

        // Get current page URL 
        $crunchifyURL = urlencode(get_permalink());

        // Get current page title
        $crunchifyTitle = str_replace( ' ', '%20', get_the_title());

        // Get Post Thumbnail for pinterest
        $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

        // Construct sharing URL without using any script
        $twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
        $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
        $googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
        $linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$crunchifyURL.'&title='.$crunchifyTitle;

        // Based on popular demand added Pinterest too
        $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;

        // Add sharing button at the end of page/page content
        $content .= '<div class="mcs-social">';
        $content .= '<h5><i class="fa fa-share-alt" aria-hidden="true"></i> SHARE <strong>ON</strong></h5> <a class="mcs-link mcs-twitter" href="'. $twitterURL .'" target="_blank"><span><i class="fa fa-twitter" aria-hidden="true"></i> Twitter</span> </a>';
        $content .= '<a class="mcs-link mcs-facebook" href="'.$facebookURL.'" target="_blank"><span><i class="fa fa-facebook" aria-hidden="true"></i> Facebook</span></a>';
        $content .= '<a class="mcs-link mcs-googleplus" href="'.$googleURL.'" target="_blank"><span><i class="fa fa-google-plus" aria-hidden="true"></i> Google+</span></a>';
        $content .= '<a class="mcs-link mcs-linkedin" href="'.$linkedInURL.'" target="_blank"><span><i class="fa fa-linkedin" aria-hidden="true"></i> LinkedIn</span></a>';
        $content .= '<a class="mcs-link mcs-pinterest" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank"><span><i class="fa fa-pinterest-p" aria-hidden="true"></i> Pin It</span></a>';
        $content .= '</div>';

        return $content;
    }else{
        // if not a post/page then don't include sharing button
        return $content;
    }
};
add_filter( 'the_content', 'mcs_social_sharing_buttons');

2 个答案:

答案 0 :(得分:1)

is_singular($post_types)与参数$post_types="your post type"

一起使用
if ( is_single( 'post' ) {
    // Do something
}

答案 1 :(得分:1)

函数is_singular()也为页面返回true,所以将is_singular()和is_single()替换为开头的if语句:

function mcs_social_sharing_buttons($content) {
global $post;
if(is_single() || is_home()){
...