No one can take away credit from installing plugins in WordPress
as plugins make life easy for blogger and even a man which don’t know
much about programming can use this option to enhance his or her blog.
But as of many thing plugins have some draw backs as well like they will
increase the loading time of your blog
and second is that customizing plugins in not that easy for an average
person. So if someone wants to be free from these plugins and want that
he or she display Random, Recent and Related Posts without plugins than
we can use some PHP code to display these posts.
How to Show Random Posts.
You can always use wordpress random posts plugin in order to show random posts on any of your page or in sidebar. But when you use this plugin you cannot customize its CSS code easily and you have to read all the code in order to customize it. Now below i am sharing a simple piece of code which will display random posts. All you have to do is to paste this code into Single.php or Sidebar.php file of your blog theme. You can change the number of posts you want to display by changing the numberposts value.
Again you can use wordpress recent posts plugin if you are not comfortable editing your blog themes files. The benefit of using your own code instead of plugin is that you can apply CSS code so that you customize these with the look and feel of your blog theme. Like you can easily apply Drop shadow effect, make corners round or even apply gradient effect. Just use the simple PHP code below into Single.php or Sidebar.php file of your blog theme.You can change the number 5 to any number of your choice.
Yet Another Related Posts Plugin (YARPP) is considered to be the best related posts plugin with lots of options. But the biggest disadvantage of using this plugin is that it takes huge amount of server resources and in some cases many blogs were blocked by there web-hosts because of excessive server resources and it turn out to be the related posts plugin which was taking many resources. So the easy solution is to use your own code which is executed relativity fast and before run time.
Related Posts without Images.
Just paste this code into any PHP file of your and it will show related posts based on tags of the current post.
Related Posts with Images.
Just paste the code given below in the Single.php file of your blog theme right after Post Content. You can change the values of image size by changing the 90 to some thing 100, 130 or may be 150.
How to Show Random Posts.
You can always use wordpress random posts plugin in order to show random posts on any of your page or in sidebar. But when you use this plugin you cannot customize its CSS code easily and you have to read all the code in order to customize it. Now below i am sharing a simple piece of code which will display random posts. All you have to do is to paste this code into Single.php or Sidebar.php file of your blog theme. You can change the number of posts you want to display by changing the numberposts value.
<li><h2>Random Posts</h2>How to Show Recent Posts.
<ul>
<?php $posts = get_posts(‘orderby=rand&numberposts=5′); foreach($posts as $post) { ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a>
</li>
<?php } ?>
</ul>
</li>
Again you can use wordpress recent posts plugin if you are not comfortable editing your blog themes files. The benefit of using your own code instead of plugin is that you can apply CSS code so that you customize these with the look and feel of your blog theme. Like you can easily apply Drop shadow effect, make corners round or even apply gradient effect. Just use the simple PHP code below into Single.php or Sidebar.php file of your blog theme.You can change the number 5 to any number of your choice.
<?php echo “<b>Recent Posts:</b>”; ?>How to Show Related Posts.
<?php get_archives(‘postbypost’, ’5′, ‘custom’, ‘<li>’, ‘</li>’); ?>
Yet Another Related Posts Plugin (YARPP) is considered to be the best related posts plugin with lots of options. But the biggest disadvantage of using this plugin is that it takes huge amount of server resources and in some cases many blogs were blocked by there web-hosts because of excessive server resources and it turn out to be the related posts plugin which was taking many resources. So the easy solution is to use your own code which is executed relativity fast and before run time.
Related Posts without Images.
Source<?php //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_title(); ?></a></p> <?php endwhile; } } ?>
Related Posts with Images.
Just paste the code given below in the Single.php file of your blog theme right after Post Content. You can change the values of image size by changing the 90 to some thing 100, 130 or may be 150.
<!——Related posts code start here——->Now its time to customize your Related Posts boxes. This time you have to copy and paste code given below in the Style.php file of your WordPress blog theme and your are ready to go. Again you can play with some of the settings to customize it according to your blog theme.
<?php
add_image_size( ‘related-posts’, 90, 90, true );
$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’=>5, // Number of related posts that will be shown.
‘caller_get_posts’=>1,
‘orderby’=>’rand’ // Randomize the posts
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo ‘<div id=”related_posts”><h3>Related Posts</h3><ul>’;
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li>
<a href=”<?php the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”>
<?php the_post_thumbnail( ‘related-posts’ ); ?>
</a>
<div>
<a href=”<? the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a>
</div>
</li>
<? }
echo ‘</ul></div>’;
} }
?>
<?php wp_reset_query(); ?>
<p/><br/>
<!——Related posts code end here——->
#related_posts {Source
margin-top:-15px;
}
#related_posts ul {
overflow:hidden;
margin:0;
margin-left:-43px;
}
#related_posts li {
list-style:none;
float:left;
margin:0 22px 0 0;
}
#related_posts li a {
display:block; font-size:11px;
font-family:tahoma, Arial, sans-serif;
text-transform:uppercase;
text-decoration:none;
letter-spacing:1px;
text-align:center;
width:100px;
line-height:16px;
border-bottom:none;
overflow:hidden; }
#related_posts li img {
padding:5px;
background-color:#f4f4f4;
border:1px solid #ddd; }
#related_posts li img:hover {
background-color:#ddd;
}
#related_posts h3 {
border-top:1px dashed #AAAAAA;
color:#135A9F;
padding-top:10px;
font-family:’Patrick Hand’,Arial,sans-serif; font-size:1.35em;
}