Add social sharing to blog page (not just single!)

Hi,
I found this functions code to add social sharing to singe posts. (my translation)

function x_add_social_sharing ( $content ) {
  if ( is_singular( array('post') ) ) {
    echo do_shortcode('[share title="Dela detta inlägg" facebook="true" twitter="true" google_plus="true" linkedin="true" email="true"]');
  }
}

But I also want to add social sharing to my blog page (listing all posts) here: https://dev.marelddesign.se/demo-blog/
(not a home page, not a front page but a page defined as the page used as blog under settings/reading i wordpress.

How do I do that?

Kindly
/MH

Hello @Holmstrom,

Thanks for asking. :slight_smile:

You can add following code in child theme function.php file:

function x_add_social_sharing() {
        echo do_shortcode('[x_share title="Share this Post" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" reddit="true" email="true"]');
}
add_action('x_before_the_excerpt_end', 'x_add_social_sharing');
add_action('x_before_the_content_end', 'x_add_social_sharing');

Before making any changes, please make sure to install child theme. I am sharing relevant resources that you can use.

https://theme.co/apex/child-themes

Thanks.

Thank you!
This also added social sharing to all pages. Is there a way to exclude those?

Hi Markus,

Please update the code given by my colleague to this:

function x_add_social_sharing() {
		if ( (!is_front_page() && is_home()) || is_single() ) {
  			echo do_shortcode('[x_share title="Share this Post 1" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" reddit="true" email="true"]');
		}
}
add_action('x_before_the_excerpt_end', 'x_add_social_sharing');
add_action('x_before_the_content_end', 'x_add_social_sharing');

Thank you.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.