Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created March 2, 2011 02:34
Show Gist options
  • Save nojimage/850365 to your computer and use it in GitHub Desktop.
Save nojimage/850365 to your computer and use it in GitHub Desktop.
WPでソーシャルボタンをハードに追加するサンプル
<?php
/* ..snip.. */
/* ここでは df_social として関数を定義していますけど適当な名前でOKです */
function df_social() {
?>
<div class="socialLink">
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="<?php the_permalink(); ?>" data-text="<?php the_title(); ?>" data-via="{あなたのアカウント}">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<a href="http://b.hatena.ne.jp/entry/<?php the_permalink(); ?>" class="hatena-bookmark-button" data-hatena-bookmark-title="<?php the_title(); ?>" data-hatena-bookmark-layout="standard" title="このエントリーをはてなブックマークに追加"><img src="http://b.st-hatena.com/images/entry-button/button-only.gif" alt="このエントリーをはてなブックマークに追加" width="20" height="20" style="border: none;" /></a><script type="text/javascript" src="http://b.st-hatena.com/js/bookmark_button.js" charset="utf-8" async="async"></script>
<iframe src="http://www.facebook.com/plugins/like.php?href=<?= urlencode(get_permalink()) ?>&amp;layout=button_count&amp;show_faces=true&amp;width=120&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:120px; height:21px;" allowTransparency="true"></iframe>
</div>
<?php
}
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<div class="content-header"><?php include('includes/content-header.inc.php'); ?></div>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'default'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a>
</h2>
<div class="postmetadata">
<?php df_get_postmetadata( array( "date", "author", "comment" ), 'span' ); ?>
</div>
<?php df_social(); /* 記事パーツ中で functions.php で定義した関数を読み出し */ ?>
<div class="entry">
<?php the_content(); ?>
</div>
<div class="postmetadata">
<?php df_get_postmetadata( array( "category", "tag", "edit" ), 'span' ); ?>
</div>
</div><!-- end of post -->
<?php endwhile; ?>
<?php df_pagenavigator('<div id="pagenavigator">', '</div>'); ?>
<?php else : /* NO posts */
if ( '' != get_404_template() )
include( get_404_template() );
else
echo( "<h3><?php _e( 'Upss, not found...', 'default' ); ?></h3>" );
endif; ?>
</div><!-- end #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
@nojimage
Copy link
Author

nojimage commented Mar 2, 2011

wp-content/themes/{some_theme_name}/functions.php にボタンを表示するための関数を追記で定義します。
( twitterのdata-via属性とかは適当に書き換えてね

あとは、
wp-content/themes/{some_theme_name}/index.phpやsingle.phpなど、各テンプレートの表示させたい部分で、functionで定義したボタン表示関数を読み出すようにします。

こんなことを自動するプラグインもあるだろうけど。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment