REALITYPOD
InternetTech

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(); ?>

<?php

$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];

};

};

endwhile;

endif;

?>

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”>
<?php
$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>
</form>

Related posts

Use and setup of iTunes home sharing

Realitypod

How the AK-47 Evolved over Time

Realitypod

Hipkey — The New Tracking Device

Realitypod