Thursday, April 9th, 2009, 1036 days ago
‘Related Posts’ hacking in wordpress
When i noticed my blog andol.info lacking some popular functions like ‘related posts’, i decided to do it myself. I googled the ‘related posts’ but most of present results are about installing plugins, no code level tips are provided. I dont like using plugins in wordpress bacause huge number of plugins probably slow down the browsing speed.
So i just hacked it myself: adding the 2nd post query-> matching the query conditions->get result & display & CSS. Some codes are shared below.
<div id=’rltpost’>
<?php
if(get_the_category()) {
echo”<p><strong> Posts may be of interest to you:</strong></p><ul>”;
foreach((get_the_category()) as $category) {
$query2 = new WP_Query(“cat=”.$category->cat_ID.”&showposts=3&orderby=rand”);
while($query2->have_posts()):
$query2->the_post();
?>
<li><a href=”<?php the_permalink() ?>” title=”permalink to: <?php the_title() ?>”><?php the_title() ?></a></li>
<?php
endwhile;
}
echo”</ul>”;
}
?>
</div>
Instruction: just copy all the codes above to where you wanna show your ‘related posts’ section, and add the CSS style in current theme’s style.css file, done, enjoy.
POSTS MAY BE OF INTEREST
