loop - Prevent function from triggering on current page

admin2025-06-02  3

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 = '='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=dica.br';
        $facebookURL = '.php?u='.$crunchifyURL;
        $whatsappURL = '/?text='.$crunchifyTitle . ' ' . $crunchifyURL;


        // Based on popular demand added Pinterest too
        $pinterestURL = '/?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="" 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="" 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' );`

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/intent/tweet?text='.$crunchifyTitle.'&amp;url='.$crunchifyURL.'&amp;via=dica.br';
        $facebookURL = 'https://www.facebook/sharer/sharer.php?u='.$crunchifyURL;
        $whatsappURL = 'https://wa.me/?text='.$crunchifyTitle . ' ' . $crunchifyURL;


        // Based on popular demand added Pinterest too
        $pinterestURL = 'https://pinterest/pin/create/button/?url='.$crunchifyURL.'&amp;media='.$crunchifyThumbnail[0].'&amp;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/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/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' );`
Share Improve this question asked Feb 24, 2019 at 20:27 RicharpowerRicharpower 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748870665a314408.html

最新回复(0)