|
<?php // don't imclude this php tag |
|
|
|
function make_related_posts() { |
|
if(is_single()) { //to only show this on single posts view |
|
$orig_post = $post; |
|
global $post; |
|
|
|
$tags = wp_get_post_tags($post->ID); |
|
if ($tags) { |
|
$tag_ids = array(); |
|
foreach($tags as $individual_tag) { |
|
$tag_ids[] = $individual_tag->term_id; |
|
} |
|
|
|
$args=array( |
|
'tag__in' => $tag_ids, |
|
'post__not_in' => array($post->ID), |
|
'posts_per_page'=>3, //Number of Posts |
|
'caller_get_posts'=>1 //To Ignore sticky post |
|
); |
|
|
|
$my_query = new wp_query( $args ); |
|
if( $my_query->have_posts() ) { |
|
echo '<div id="relatedpostscover">'; |
|
while( $my_query->have_posts() ) { |
|
$my_query->the_post(); ?> |
|
<div class="relatedpostscontent"> |
|
<div class="relatedpostspic"> |
|
<?php |
|
$first_img = get_first_image(); //call to function fetching first image of post |
|
if($first_img != '') { |
|
?> |
|
<a href="<? the_permalink()?>" title="<?php the_title(); ?>"><img src="<?php echo $first_img ?>" /></a> |
|
<?php |
|
} else { |
|
?> |
|
<a href="<? the_permalink()?>" title="<?php the_title(); ?>"><img src=" XYZ " /></a> // replace XYZ with path of your default image |
|
<?php |
|
} |
|
?> |
|
</div> |
|
<div class="relatedpoststitle"> |
|
<p class="rps_title"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p> |
|
<p class="rps_date"><?php the_time('M j, Y') ?></p> |
|
</div> |
|
<div class="relatedpostsshare"> |
|
<span id="fb-button"> |
|
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&layout=button_count&show_faces=false& width=450&action=like&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:80px; height:22px; position: relative; top: 2px;"> |
|
</iframe> |
|
</span> |
|
<span id="tweet-button"> |
|
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script> |
|
<a href="http://twitter.com/share" class="twitter-share-button" |
|
data-url="<?php the_permalink(); ?>" |
|
data-via=" XYZ " //replace XYZ with your official twitter username |
|
data-text="<?php the_title(); ?>" |
|
data-related=" XYZ " //replace XYZ with your Site Name |
|
data-count="horizontal">Tweet |
|
</a> |
|
</span> |
|
</div> |
|
</div> |
|
<? |
|
} |
|
echo '</div>'; |
|
} |
|
} |
|
$post = $orig_post; |
|
wp_reset_query(); |
|
} |
|
} |
|
|
|
//famous catch_that_plugin snippet in action |
|
|
|
function get_first_image() { |
|
global $post; |
|
$first_img = ''; |
|
ob_start(); |
|
ob_end_clean(); |
|
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); |
|
$first_img = $matches[1][0]; //path of first image occuring in the_content |
|
return $first_img; |
|
} |
|
|
|
add_action( 'genesis_after_entry','make_related_posts' ); |