admin管理员组文章数量:1122826
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.
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.
Share Improve this question edited Jan 24, 2021 at 9:58 dragoweb asked Jan 22, 2021 at 23:29 dragowebdragoweb 2494 silver badges13 bronze badges 5 |1 Answer
Reset to default 0Alright, 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?).
本文标签: pluginsAddToAny shortcode in the loop
版权声明:本文标题:plugins - AddToAny shortcode in the loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281717a1926407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_
such asthe_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 ♦ Commented Jan 23, 2021 at 0:00generate_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 asecho ""; 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 ♦ Commented Jan 23, 2021 at 12:57