Top 10 WordPress Hacks

No 4. Automatically get images on post content

This code can display relevant images on your posts according the content and there is no plugin to do what this code can do.

Use the following code anywhere in your theme:

<?php if (have_posts()) : ?>

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


$szPostContent = $post->post_content;

$szSearchPattern = ‘~<img [^>]* />~’;

// Run preg_match_all to grab all the images and save the results in $aPics

preg_match_all( $szSearchPattern, $szPostContent, $aPics );

// Check to see if we have at least 1 image

$iNumberOfPics = count($aPics[0]);

if ( $iNumberOfPics > 0 ) {

// Now here you would do whatever you need to do with the images

// For this example the images are just displayed

for ( $i=0; $i < $iNumberOfPics ; $i++ ) {

echo $aPics[0][$i];






No 3. Categories drop down feature

Using the following code you can add a nice little drop down box that will list all the categories in you blog. Use the following code in sidebar.php file or anywhere in the index.php:

<form action=”<?php bloginfo(‘url’); ?>/” method=”get”>
$select = wp_dropdown_categories(‘show_option_none=Select category&show_count=1&orderby=name&echo=0′);
$select = preg_replace(“#<select([^>]*)>#”, “<select$1 onchange=’return this.form.submit()’>”, $select);
echo $select;
<noscript><input type=”submit” value=”View” /></noscript>

Related posts

MAME Emulation in 2021: The Definitive Guide to Getting Started

Adam Kull

Stay Pumped While Swimming Like Never Before With The FINIS Duo Music Player


Resume/ CV Eye Heat Map