1

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">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="sfr-inner"> 
                    <div><?php if ( has_post_thumbnail() ) { ?><div class="sfr-thumb"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail("medium") ?></a></div><?php } ?></div>
                    <div><p><?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.

5
  • 1
    Functions beginning with the_ such as the_permalink do not return the permalink, they echo the permalink, and can't be used that way, your code does not insert the permalink or title into that string. Where is this code located? Is it inside a post loop? Or outside? – Tom J Nowell Jan 23 at 0:00
  • Thanks Tom, the code is inside the functions.php child theme. I just edited my post with the full code. I continue investigate... – dragoweb Jan 23 at 9:41
  • Did you ask this question on the addtoany support forum? – Q Studio Jan 23 at 10:31
  • 1
    @dragoeco what is the generate_before_do_template_part filter? Is that a Generatepress hook? If so you need to contact their support, normal answers will not apply to you. I also see that you've made more mistakes of the same kind, things that output, vs things that return. echo do_shortcode( is the same as echo ""; do_shortcode(, they are not the same. I ****strongly*** recommend reading up some basic PHP tutorials as this is an important thing you need to understand to avoid these problems – Tom J Nowell Jan 23 at 12:57
  • @tom-j-nowell thanks for the advice. I'm still learning PHP and I see what you mean now. I made the appropriated changes (added get_) in my code. Also, the shortcode is place in 2 different custom loop pages. One generated on the theme (Generatepress) and another one to a plugin (searchandfilter), so I guess the problem is not related to either of them. I just noticed that the returned HTML by the shortcode is correct so it must be something related with the plugin I guess. So, as Qstudio suggested, I just posted the question to the Plugin support page. Let's see if I get an answer... – dragoweb Jan 23 at 16:02
0

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?).

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.