Skip to main content

Useful Hacks For WordPress Theme Development

Developing WordPress theme, we have to write few common functionality repeatedly like social media sharing, popular posts, display author information etc. These things sometimes makes us frustrate. So here in this post I am going to give you major WordPress code hacks that you can use in your code which will save you a lot of time as well as speed-up development.


Display Author Information For Posts

By putting the codes below in your single.php file, you can display the information from the post author’s profile on your blog and show a bit about them. This is especially useful if you’ve got a lot of authors on your blog.

  1. <div id=“author-info”>
  2. <div id=“author-image”>
  3. <a href=“<?php the_author_meta(‘user_url’); ?>”><?php echo get_avatar( get_the_author_meta(‘user_email’), ’80’ ); ?></a>
  4. </div>
  5. <div id=“author-bio”>
  6. <h4>Written by <?php the_author_link(); ?></h4>
  7. <?php the_author_meta(‘description’); ?>
  8. </div>
  9. </div>


Add Post Loop

You can use the below code to show 10 most recent posts from a category “featured”. you can also change the showpost=5 to whatever you had like and also change the category name. It is a very useful if you are developing a custom home page with recent posts from different categories. This code will be put anywhere in your themes files (sidebar.php, footer.php, header.php, etc)

  1. <?php $my_query = new WP_Query(‘category_name=Featured&showposts=10’);
  2. while ($my_query->have_posts()) : $my_query->the_post();
  3. $do_not_duplicate = $post->ID; ?>
  4. <!– POST CODES HERE –>
  5. <?php endwhile; ?>


Showing Related Posts Without A Plugin

By using below code in your “signle.php” file, you can display related posts that will be based on related tags.

  1. <?php
  2. $tags = wp_get_post_tags($post->ID);
  3. if ($tags) {
  4. $tag_ids = array();
  5. foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  6. $args=array(
  7. ‘tag__in’ => $tag_ids,
  8. ‘post__not_in’ => array($post->ID),
  9. ‘showposts’=>5, // Number of related posts that will be shown.
  10. ‘caller_get_posts’=>1
  11. );
  12. $my_query = new wp_query($args);
  13. if( $my_query->have_posts() ) {
  14. echo ‘
  15. <h3>Related Posts</h3>
  16. <ul>’;
  17. while ($my_query->have_posts()) {
  18. $my_query->the_post();
  19. ?>
  20. <li><a href=“<?php the_permalink() ?>” rel=“bookmark” title=“Permanent Link to < ?php the_title_attribute(); ?>”>< ?php the_title(); ?></a></li>
  21. < ?php
  22. }
  23. echo ‘</ul>’;
  24. }
  25. }
  26. ?></ul>


Showing Popular Posts

You can also display the popular post in your sidebar.php file. The popular post will be based on the comment count for the posts.

  1. <h2>Popular Posts</h2>
  2. <ul>
  3. <?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5”);
  4. foreach ($result as $post) {
  5. setup_postdata($post);
  6. $postid = $post->ID;
  7. $title = $post->post_title;
  8. $commentcount = $post->comment_count;
  9. if ($commentcount != 0) { ?>
  10. <li><a href=“<?php echo get_permalink($postid); ?>” title=“< ?php echo $title ?>”>< ?php echo $title ?></a> {< ?php echo $commentcount ?>}</li>
  11. <?php } } ?></ul>


Returns The Visitor To The Page They were Logged

Below is the code that returns the person to the page they were currently viewing when logged in.

  1. <?php if(!is_user_logged_in()) { ?>
  2. <form action=“<?php echo wp_login_url(get_permalink()); ?>” method=“post”>
  3. <label for=“log”><input type=“text” name=“log” id=“log” value=“<?php echo wp_specialchars(stripslashes($user_login), 1) ?>” size=“22” /> User</label>
  4. <label for=“pwd”><input type=“password” name=“pwd” id=“pwd” size=“22” /> Password</label>
  5. <input type=“submit” name=“submit” value=“Send” class=“button” />
  6. <label for=“rememberme”><input name=“rememberme” id=“rememberme” type=“checkbox” checked=“checked” value=“forever” /> Remember me</label>
  7. </form>
  8. <?php } ?>


Add Social Media Without A Plugin

Now every website need to add social media links like facebook, twitter etc. by using below code into your single.php file, you will be able to go with links for twitter, digg, delicious, reddit and stumbleupon and also add many more.

  1. <ul class=“socialwrap”>
  2. <li><a href=“;url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Digg This Post</a></li>
  3. <li><a href=“ <?php the_title(); ?> < ?php bloginfo(‘home’); ?>/?p=< ?php the_ID(); ?>”>Tweet This Post</a></li>
  4. <li><a href=“<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Stumble This Post</a></li>
  5. <li><a href=“<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Save on Delicious</a></li>
  6. <li><a href=“<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Submit to Reddit</a></li>
  7. </ul>

Hope you found this post useful! If you have any other hacks? Please share with us!

Leave a Reply

Your email address will not be published. Required fields are marked *


Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!