How to make a new function that includes a template

I am adding social share buttons to all of my posts. Is there a way to put the code into a function so I can just call that function when I went to put the social share buttons somewhere?

This is the social share buttons code…

                <a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-twitter.png"></a>

                <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank" title="Share on Facebook."><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-facebook.png"></a>

                <a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>" target="_blank"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-pinterest.png"></a>

                <a href="http://www.bloglovin.com/e/?b=' .get_bloginfo('wpurl') . '&p=' .get_permalink().'&t='.get_the_title().'"onclick=\'window.open(this.href,&quot;bloglovin_like&quot;,&quot;height=320,width=480,toolbar=0,location=0,menubar=0,scrollbars=0,status=0&quot;); return false;\'--><img style="margin: 10px 0 10px 0; border: 0; padding: 0;" src="http://localhost:8888/wordpress/wp-content/uploads/2016/10/social-bloglovin-1.png" alt="" />

                <a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://localhost:8888/wordpress/wp-content/uploads/2016/12/social-shopping.png"></a>

Here is my frontage.php I want to put the social share buttons before the Read More button of each post. However if I placed the social share code before each of these I would have to paste in the code quite a few times. So I was wondering if there is a more efficient way to do this? (I know I already have a few functions in this code – that is because I had help some help creating this code – I’m still new to this and can’t quite figure out exactly how to do this on my own)

<?php
/*
 * Template Name:
 */

get_header();
get_template_part ('inc/carousel');

$the_query = new WP_Query( [
    'posts_per_page' => 14,
    'paged' => 1
] );
<div id="ajax">
if ( $the_query->have_posts() ) {
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
            <article <?php post_class( 'col-md-12' ); ?>>
                <?php the_post_thumbnail('large-thumbnail'); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( 'share-buttons' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
            </article><?php

        } else { // Small posts ?>

            <article <?php post_class( 'col-md-4' ); ?>>
                <?php the_post_thumbnail( 'medium-thumbnail' ); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( 'share-buttons' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
    </article>
<?php 
        }
        $i++;
    }
    wp_reset_postdata();
    </div>
}
else {
    echo '<p>Sorry, no posts matched your criteria.</p>';
}
<?php load_more_button(); ?>
get_footer();

This is the layout of the front page
enter image description here

Solutions Collecting From Web of "How to make a new function that includes a template"

Add your share button html to a template file in your theme:

your-theme/share-buttons.php

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://img.mywordpresstips.com/functions/social-twitter.png"></a>

<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" target="_blank" title="Share on Facebook."><img src="http://img.mywordpresstips.com/functions/social-facebook.png"></a>

<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>" target="_blank"><img src="http://img.mywordpresstips.com/functions/social-pinterest.png"></a>

<a href="http://www.bloglovin.com/e/?b=' .get_bloginfo('wpurl') . '&#038;p=' .get_permalink().'&#038;t='.get_the_title().'"onclick=\'window.open(this.href,&quot;bloglovin_like&quot;,&quot;height=320,width=480,toolbar=0,location=0,menubar=0,scrollbars=0,status=0&quot;); return false;\'--><img style="margin: 10px 0 10px 0; border: 0; padding: 0;" src="http://img.mywordpresstips.com/functions/social-bloglovin-1.png" alt="" />

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank" title="Tweet this!"><img src="http://img.mywordpresstips.com/functions/social-shopping.png"></a>

Use get_template_part() to include the template in the appropriate locations.

get_template_part( 'share-buttons' );

Edit: Here’s an updated version of the template code which should address some issues in the original. It includes the share buttons template part too:

<?php
/*
 * Template Name:
 */

get_header();
get_template_part ('inc/carousel');

$the_query = new WP_Query( [
    'posts_per_page' => 14,
    'paged' => 1
] );

if ( $the_query->have_posts() ) {
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and after every block of 6 posts... ?>

            <article <?php post_class( 'col-md-12' ); ?>>
                <?php the_post_thumbnail('large-thumbnail'); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( 'share-buttons' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
            </article><?php

        } else { // Small posts ?>

            <article <?php post_class( 'col-md-4' ); ?>>
                <?php the_post_thumbnail( 'medium-thumbnail' ); ?>
                <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <?php get_template_part( 'share-buttons' ); ?>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
            </article><?php 
        }
        $i++;
    }
    wp_reset_postdata();
}
else {
    echo '<p>Sorry, no posts matched your criteria.</p>';
}

get_footer();