WordPress is almost a super blogging tool right now we have, the main advantage of using wordpress is all about plugins and hacks. I have made list of 10 must have wordpress hacks for your blog. Download or Apply it free.

1. Avoid Duplicate Posts in Multiple Loops

  1. Let’s start by creating a simple PHP array, and put all post IDs from the first loop in it.
    01 <h2>Loop n°1</h2>
    02
    03 <?php
    04 $ids = array();
    05 while (have_posts()) : the_post();
    06 the_title();
    07 ?>
    08 <br />
    09
    10 <?php $ids[]= $post->ID;
    11 endwhile; ?>
  2. Now, the second loop: we use the PHP function in_array() to check if a post ID is contained in the $ids array. If the ID isn’t contained in the array, we can display the post because it wasn’t displayed in the first loop.
    1 <h2>Loop n°2</h2>
    2 <?php
    3 query_posts("showposts=50");
    4 while (have_posts()) : the_post();
    5 if (!in_array($post->ID, $ids)) {
    6 the_title();?>
    7 <br />
    8 <?php }
    9 endwhile; ?>

2. Replacing “Next” and “Previous” Page Links with Pagination

  1. The first thing to do, obviously, is download the plug-in.
  2. Unzip the plug-in archive on your hard drive, and upload the wp-pagenavi.php and wp-pagenavi.css files to your theme directory.
  3. Open the file that you want the pagination to be displayed in (e.g. index.php, categories.php, search.php, etc.), and find the following code:
  4. <div>
    <div><?php next_posts_link('Previous entries') ?></div>
    <div><?php previous_posts_link('Next entries') ?></div>
    </div>

    Replace this part with the code below:

    1 <?php
    2 include('wp-pagenavi.php');
    3 if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    4 ?>
  5. Now we have to hack the plug-in file. To do so, open the wp-pagenavi.php file and find the following line (line #61):
    1 function wp_pagenavi($before = '', $after = '') {
    2 global $wpdb, $wp_query;

    We have to call the pagenavi_init() function, so let’s do it this way:

    1 function wp_pagenavi($before = '', $after = '') {
    2 global $wpdb, $wp_query;
    3 pagenavi_init(); //Calling the pagenavi_init() function
  6. We’re almost done. The last thing to do is to add the wp-pagenavi style sheet to your blog. To do so, open up header.php and add the following line:
    <link rel="stylesheet" href="<?php echo TEMPLATEPATH.'/pagenavi.css';?>" type="text/css" media="screen" />

3. Automatically Get Images on Post Content

  1. Paste the following code anywhere in your theme.
    01 <?php if (have_posts()) : ?>
    02 <?php while (have_posts()) : the_post(); ?>
    03
    04 <?php
    05 $szPostContent = $post->post_content;
    06 $szSearchPattern = '~<img [^>]* />~';
    07
    08 // Run preg_match_all to grab all the images and save the results in $aPics
    09 preg_match_all( $szSearchPattern, $szPostContent, $aPics );
    10
    11 // Check to see if we have at least 1 image
    12 $iNumberOfPics = count($aPics[0]);
    13
    14 if ( $iNumberOfPics > 0 ) {
    15 // Now here you would do whatever you need to do with the images
    16 // For this example the images are just displayed
    17 for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
    18 echo $aPics[0][$i];
    19 };
    20 };
    21
    22 endwhile;
    23 endif;
    24 ?>

4. Display a Random Header Image on Your WordPress Blog

  1. Once you have selected some images to be your header images, name them 1.jpg, 2.jpg, 3.jpg and so on. You can use as many images as you want.
  2. Upload the images to your wp-content/themes/yourtheme/images directory.
  3. Open header.php and paste the following code in it:
    1 $num = rand(1,10); //Get a random number between 1 and 10, assuming 10 is the total number of header images you have
    2 <div id="header" style="background:transparent url(images/.jpg) no-repeat top left;">
  4. You’re done! Each page or post of your blog will now display a random header image.

5. Insert Ads (or Anything Else) in Your RSS Feed

Follow these simple steps to perform this hack:

  1. Edit the functions.php file of your theme. If your theme doesn’t have a functions.php file, simply create one.
  2. Paste the following code into your functions.php file:
    1 <?php
    2 function insertAds($content) {
    3 $content = $content.'<hr /><a href="http://www.wprecipes.com">Have you visited WpRecipes today?</a><hr />';
    4 return $content;
    5 }
    6 add_filter('the_excerpt_rss', 'insertAds');
    7 add_filter('the_content_rss', 'insertAds');
    8 ?>
  3. Save the file. You’re now displaying your ads in your RSS feed!

Special thanks to smashing magazine for this wonderful hacks

Waqar Hussain

a project management professional and blogger who write about project management and entrepreneurship in Pakistan

Website - Twitter - Facebook - More Posts

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>