Useful Hacks For WordPress Development

June 6th, 2012 by Laeeq | 8 comments

During WordPress theme Development, 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=“http://digg.com/submit?phase=2&amp;url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Digg This Post</a></li>
  3. <li><a href=“http://twitter.com/home/?status=Reading <?php the_title(); ?> < ?php bloginfo(‘home’); ?>/?p=< ?php the_ID(); ?>”>Tweet This Post</a></li>
  4. <li><a href=“http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Stumble This Post</a></li>
  5. <li><a href=“http://delicious.com/post?url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>”>Save on Delicious</a></li>
  6. <li><a href=“http://www.reddit.com/submit?url=<?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!

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Parse HTML/Web Pages with PHP
  • Block Multiple IP Addresses Using PHP
  • How much memory do PHP variables use?
  • Manipulating PHP arrays: push, pop, shift, unshift
  • 10 Super Useful PHP Code Snippets
  •  

     

    1. June 7th, 2012 at 07:44 | #1

      It’s a good post.

    2. June 7th, 2012 at 10:46 | #2

      It’s a good post.

    3. June 9th, 2012 at 12:21 | #3

      Wow! Finally I got a web site from where I can truly get useful information regarding my study and knowledge.

    4. June 10th, 2012 at 07:58 | #4

      Cool yr
      Thnks for this

    5. June 13th, 2012 at 17:49 | #5

      I regard something really interesting about your site so I bookmarked .

    6. June 24th, 2012 at 15:00 | #6

      really nice!

    7. August 4th, 2012 at 22:18 | #7

      It’s a good post.

    8. lancel outlet
      March 20th, 2013 at 05:32 | #8

      I would like more information about this, because it is very nice., Thanks for sharing

    1. No trackbacks yet.