WordPress.org

Forums

Smart Website Tools by AddThis
Buttons not showing when page includes a shortcode with custom post type loop (11 posts)

  1. mobilewebexpert
    Member
    Posted 10 months ago #

    This really simple shortcode function is stopping the AddThis buttons from showing...

    function mwe_rtr_do_shortcode_2() {
    
        $args = array(
            'post_type' => 'mwe_rtr', //my custom post type
            'posts_per_page' => -1,
            'order' => 'ASC',
            'order_by' => 'date'
        );
        $loop = new WP_Query( $args );
    
        //loop through custom post types
        while ( $loop->have_posts() ) : $loop->the_post();
            //do nothing
        endwhile;
    
        return 'Only if the above while loop is removed will the AddThis buttons be shown on any page that uses this [mwe_rtr_2] shortcode!';
    }
    add_shortcode( 'mwe_rtr_2', 'mwe_rtr_do_shortcode_2' );

    It's the presence of the while loop in my code that is causing the AddThis buttons not to show.

    This seems like a bug with AddThis, so can someone solve or advise?

    Thanks in advance,
    James

    PS - All my plugins are up-to-date and I have set WP_DEBUG to TRUE in wp-config.php, but no clues are outputted as to the problem.

    https://wordpress.org/plugins/addthis/

  2. Srijith V
    AddThis Samurai
    Plugin Author

    Posted 10 months ago #

    Hi James,

    Can I see the page with the issue? I will have a look at and let you know.

  3. Srijith V
    AddThis Samurai
    Plugin Author

    Posted 10 months ago #

    Hi James,

    Looks like global variable $post has been overridden by your shortcode script.

    Can you try changing the following code in the AddThis plugin.

    File: wp-content/plugins/addthis/addthis_social_widget.php

    Function: addthis_display_social_widget($content, $filtered = true, $below_excerpt = false)

    Line number: 1478

    change

    elseif ( is_page($post->ID) )

    to

    elseif ( is_page($post->ID) || get_query_var('page_id') ).

    Hope this will resolve the issue.

  4. mobilewebexpert
    Member
    Posted 10 months ago #

    Hiya Srijith,

    Thanks for your reply.

    That doesn't work, but when I change that line to...
    elseif ( is_page($post->ID) || get_query_var('page_id') || true )
    ...then it does work, but what is the proper solution?

    Also, the social buttons do not show on another page where I use a shortcode. The two pages in question are:
    http://www.chemtrailsprojectuk.com/evidence/rainwater-test-kit-results-map/
    http://www.chemtrailsprojectuk.com/take-action/directive/

    Thanks,
    James

  5. mobilewebexpert
    Member
    Posted 10 months ago #

    Can also confirm that my shortcode script doesn't override $post - see my code snippet, above.

    As mentioned in the comment in my code snippet, it is the presence of the loop that appears to be causing the problem.

  6. Srijith V
    AddThis Samurai
    Plugin Author

    Posted 10 months ago #

    Hi James,

    Your if condition,
    elseif ( is_page($post->ID) || get_query_var('page_id') || true )
    will make it always a true condition, which is not a recommended solution.

    It has worked without that true condition in my localhost.

    Could you please check, Show AddThis on pages has been ticked, in the Advanced Settings page from admin.

  7. mobilewebexpert
    Member
    Posted 10 months ago #

    Hiya Srijith,

    Thanks for your reply.

    I know it is not a recommended solution. I just tried it to check the AddThis icons would actually show.

    Incidentally, when I leave the "|| true" clause in, the AddThis icons do not show on this aforementioned page - http://www.chemtrailsprojectuk.com/take-action/directive/ . However, they do show on this aforementioned page - http://www.chemtrailsprojectuk.com/evidence/rainwater-test-kit-results-map/ - but with the number-of-shares values all zero (whereas they were previously all above zero before).

    And, yes, I have gone into AddThis advanced settings and the Pages checkbox is ticked. I have also unticked it, saved, reticked it, saved... but your code change is still not working for me.

    Thanks,
    James

  8. mobilewebexpert
    Member
    Posted 10 months ago #

    Something has just dawned on me, but I'm not sure if it would/should be an issue...

    The AddThis settings may have been tied to my facebook account, but I came off facebook fairly recently.

    That said, the AddThis icons are showing fine on news posts - e.g., http://www.chemtrailsprojectuk.com/petition-ban-geoengineering-delivered/ .

  9. Srijith V
    AddThis Samurai
    Plugin Author

    Posted 10 months ago #

    Hi James,

    I checked your page http://www.chemtrailsprojectuk.com/take-action/directive/. The reason the buttons are not showing is that our javascript is not loaded in the page.

    I am not sure about the reason, but can happen due to a conflict with another plugin or theme.

    A quick fix is to add this to your theme file:

    <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=YOUR_PUB_ID_HERE"></script>

  10. mobilewebexpert
    Member
    Posted 10 months ago #

    OK, thanks for the reply.

    I have added that line of code just before my </head> tag in my header.php file.

    The result is that this page - http://www.chemtrailsprojectuk.com/take-action/directive/ - now shows the AddThis icons.

    But the other page - http://www.chemtrailsprojectuk.com/evidence/rainwater-test-kit-results-map/ - still does not show the AddThis icons. And if you look at the source code for this page, you will see that addthis_widget.js script is now called twice for this page.

  11. jpresley23
    Member
    Posted 9 months ago #

    Sorry for butting in, but I just came across this. Mobilewebexpert, from the code you shared, it looks like you don't reset the post data, which would result changing the $post variable. Take a look at wp_reset_postdata and Nested Loops for more information.

Reply

You must log in to post.

About this Plugin

  • Smart Website Tools by AddThis
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic