I’m afraid I cannot replicate your problem.
I’m uncertain as to why it is going wrong as I cannot see the loop that is being used, but a way that you could probably ensure it works is to adjust your function to accept a $post input:
//Social Media
function showSocialButtons( $post = null ) {
if( ! $post ){
global $post;
}
//The output
}
You will need to change your functions to reflect this though. For example:
the_title();
would become
echo get_the_title( $post->ID );
or simply
echo $post->post_title;
Furthermore, it’s unlikely it’s what’s causing the trouble but I don’t think you’re using action hooks for their intended purpose. Actions in WP should be considered similar to events in javascript (but with synchronocity maintained). How you’re currently using it is a long-winded route to something the same as an argument-free function call.
In general, actions should be placed inside functions or in an important part of the script so that multiple functions can be attached to that occasion:
function showSocialMediaButtons(){
//Some code here
do_action( 'social_media_buttons_shown' );
}
combined with:
add_action( 'social_media_buttons_shown', 'myFuncToDoAfterEveryShowingOfSocialMediaButtons' );
will mean that everytime
showSocialMediaButtons();
is called,
myFuncToDoAfterEveryShowingOfSocialMediaButtons();
will be called immediately after.
Anyway, in your situation, rather than using
//functions.php
add_action( 'show_social_buttons', 'showSocialButtons' );
and
//template.php
do_action( 'show_social_buttons' );
Just use
//template.php
showSocialButtons();
or, if you’ve changed the function to use the $post variable as an argument as I suggested above:
//template.php
$q = new WP_Query( $args );
while( $q->have_posts() ): $q->next_post(); //or however you've done your loop
.
.
.
showSocialButtons( $q->post ); //or whatever is referencing the post at this time.
.
.
.
endwhile;
I hope this helps.