Display related posts in wordpress without plugin

How to display related post in wordpress without plugin? Displaying related posts in wordpress blog is a very great way to help the visitors staying longer on your blog. It is also can increase your blog page views. You can use a related post plugin, but you also can use tags and custom code to show related posts in your wordpress. Let’s see how to do that.

Copy this code to your wordpress theme code anywhere to show related post:

<?php
$this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts(’numberposts=6&offset=0&orderby=post_date&order=DESC&category=’.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 5) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>

<?php if ( $posts ) : ?>
<div class=”related_articles”>
<b>Related Posts</b>
<ul>
<?php foreach ( $posts as $post ) : ?>
<li><a href=”<?php the_permalink() ?>”
title=”<?php echo trim(str_replace(”n”,” “,preg_replace(’#<[^>]*?>#si’,”,get_the_excerpt()))) ?>”>
<?php if ( get_the_title() ){ the_title(); }else{ echo “Untitled”; } ?></a>
(<?php the_time(’F jS, Y’) ?>)</li>
<?php endforeach // $posts as $post ?>
</ul>
</div>
<?php endif // $posts ?>
<?php
$post = $this_post;
unset($this_post);
?>

That code work to display related post in your wordpress blog, i hope this is useful and thanks.

display authors gravatar in wordpress posts

We know the gravatar can display in wordpress comments, however how to display or add author’s gravatar in your wordpress post? Sure, you can use a well known wordpress hack to get the post author gravatar  and display it to your wordpress posts with easily

Simply paste the following code to display author gravatar on your single.php file where you want the gravatar to be displayed:

<?php
	$author_email = get_the_author_email();
	echo get_avatar($author_email, '96');
?>

That’s easy as that, may be useful.

how to number of comments in wordpress

Your wordpress blog post have lots of comments? If yes, it can be really useful for you and also your blog readers to number the comments. Here’s how to number of comments in wordpress easily and efficiently.

Open your wordpress template on comments.php and find the following line:

<?php foreach ($comments as $comment) : ?>

Just above this line, initialize counter variable:

<?php $i = 0; ?>

Just after this line, increment counter:

<?php $i++; ?>

Now, you just have to echo the $i variable to get the number of the comment. Paste this code anywhere on the comments loop:

<?php echo $i; ?>

Your wordpress comments are now numbered. Hope this is useful for you.

Get Random Post in wordpress

Sometimes we want to display random post in wordpress. So How do I show random post on wordpress. Copy following code above and snippet to your wordpress theme file:

<?php query_posts(’orderby=rand&showposts=5?); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<h1>

<a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a>

</h1>

<?php the_content(); ?>

<?php endwhile; ?>

<?php endif; ?>

You can snippet code above in your wordpress theme anywhere, in Index, sidebar or on other pages. You can change showposts = 5 with a number up to how many posts are you want to show in random.

You can also add a category-id parameter if only want to display a certain category, for instance cat =1

And for the_content () Can you replace with the_excerpt () if you just want to show the excerpt only.

It’s so easy to show random post in wordpress. Good luck and have a nice blogging

Create wordpress page templates

Page template is very useful, they allow you to create custom page, for example creating an archive page, link page and much more, so what should you do to create page template to your wordpress?

Read the rest of this entry »