I want to add social share buttons to my posts, but I don’t want to put all that big bulk of code into all my content files. I want to create a function, and then add the function name to my template files, something like this:
function pietergoosen_sosiale_netwerk_deel_knoppies() {
<strong>Deel die pos met ander</strong><p </p>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="Share on Facebook.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" /></a>
<a href="http://twitter.com/home/?status=<?php the_title(); ?> : <?php the_permalink(); ?>" title="Tweet this!">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/twitter.png" alt="Tweet this!" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="Google+1.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/google.png" alt="Google+1" id="Google+1" /></a>
<a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="StumbleUpon.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/stumbleupon.png" alt="StumbleUpon" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="Digg this!">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/digg.png" alt="Digg This!" /></a>
<a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="Bookmark on Delicious.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/delicious.png" alt="Boekmerk op Delicious" /></a>
<a href="https://mail.google.com/mail/?view=cm&fs=1&to&su=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="epos.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/gmail.png" alt="epos" id="epos dit" /></a> }
This isn’t working. How should I do it correctly?
There are a couple of problems with your code:
function foo(){ ?><strong><?php }
<a href
more than once is a bug.get_the_title()
in attributes. Usethe_title_attribute( array ( 'echo' => FALSE ) )
instead, or you can get unexpected markup into your HTML output.bloginfo('stylesheet_directory')
. Useget_stylesheet_directory_uri()
in child themes only andget_template_directory_uri()
in all other cases.&
.https://delicious.com/post?
. It has been that for years.The resulting code could look like this:
Now you can call this function in a template:
First of all, you never closed your php tags.
Second of all, do not call functions each time for each social icon. Save values in the variables instead to speed up the process.
It would go something like this: