AddToAny shortcode in the loop

I try to add addtoany shortcode in my custom loop page but the shared link stays always the same for each posts in the loop.

?php 
global $post; 
echo do_shortcode('[addtoany url=' . the_permalink() . ' title=' . the_title() . ']') 
?

I also tried with get_permalink(), get_permalink($post-ID), get_the_permalink()... but nothing works. It should work like this, according the official documentation.

I don't understand why :/

EDIT

Here is the full function:

?php
add_filter( 'generate_do_template_part', function( $do ) {
    if ( is_archive() || is_search() ) {
        return false;
    }
    return $do;
});
add_action( 'generate_before_do_template_part', function() {
    global $post;
    if ( is_archive() || is_search() ) : ?
        article ?php post_class(); ?
            div class=inside-article sf-result
                h2a href=?php the_permalink(); ??php the_title(); ?/a/h2
                div class=sfr-inner 
                    div?php if ( has_post_thumbnail() ) { ?div class=sfr-thumba href=?php the_permalink() ??php the_post_thumbnail(medium) ?/a/div?php } ?/div
                    divp?php the_excerpt(); ?/p/div
                    div class=sfr-inner-bottom
                        span class=mdi mdi-share/span!-- share icon --
                        div class=sfr-share-box?php echo do_shortcode('[addtoany url=' . get_the_permalink() . ' title=' . get_the_title() . ']') ?/div!-- opens by a click with a jQuery function --
                    /div
                /div
            /div
        /article
    ?php endif;
}); ?

With this function, I override the loop in archive and search page generate by GeneratePress theme (and searchanfilter plugin). I get the correct permalink for the title, the thumbanil and the excerpt without problem, but not inside this shortcode.

Topic shortcode permalinks plugins Wordpress

Category Web


Alright, I got it. And it seems so obvious to me now, that I feel a little ashamed (I'm probably tired). I spent 2 days on this issue before realizing I made a big mistake.

I used this jQuery to open the .sfr-share-box' modal containing the share icons (with a full width overlay):

$('.sfr-share').on('click', function() {
    $('.sfr-share-box').fadeIn();
}); 

and so, as you have probably already guessed, ALL modals opened at the same time, one on top of the other. And so it was only the last one that appeared, and that's why I always had the same sharing link.

I modified the function and now everything is working fine.

$('.sfr-share').on('click', function() {
    $(this).closest('.sf-result').children('.sfr-share-box').fadeIn();
});

I probably deserve a negative vote because it's so stupid, but I'm glad I finally found it ^^ Anyway, thanks guys for your advices (we always learn from our mistakes, right?).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.