0

I created a list of posts, each post about a car model. After that, I created a post about the best car models. This post grabs each post about every car model and displays them in a snippet list format inside the main post "Best car models".

I am using the function below to display social sharing buttons. I would like to exclude the current post from displaying the social buttons so that the buttons show only on the post snippets.

Sth like getting the current post id and using an if statement to filter it. Thanks.

`function crunchify_social_sharing_buttons($content) {

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

        // Get current page title
        $crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');
        // $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=dica.com.br';
        $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
        $whatsappURL = 'https://wa.me/?text='.$crunchifyTitle . ' ' . $crunchifyURL;


        // 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 .= '<ul class="ul">';
        $content .= '<li class="twitter"> <a target="_blank" rel="noopener nofollow" href="'. $twitterURL .'" target="_blank"><svg class="icon text-btn-icon svg-inline--fa fa-w-18 fa-fw" style="fill: #fff; "><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-twitter"></use></svg> <span>Twitter </span></a>';
        $content .= '<li class="facebook"> <a target="_blank" rel="noopener nofollow" href="'.$facebookURL.'" target="_blank"><svg class="icon text-btn-icon svg-inline--fa fa-w-18 fa-fw" style="fill: #fff; "><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-facebook-share"></use></svg> <span>Facebook </span></a>';
        $content .= '<li id="hidewhatsapp" class="whatsapp"> <a href="'.$whatsappURL.'" target="_blank"><svg class="icon text-btn-icon svg-inline--fa fa-w-18 fa-fw" style="fill: #fff;  viewBox="0 0 100 100" class="share-icon"> <use xlink:href="#icon-whatsapp"></use></svg> <span> WhatsApp </span></a>';




        $content .= '</ul> </div>';


        return $content;



}
add_shortcode( 'share-buttons', 'crunchify_social_sharing_buttons' );`
0

If you are using a standard WordPress loop to output the list of posts, you can compare get_the_ID() to get_queried_object_id(), they will be the same for the current post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.