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.