RSS Feeds

Archive for November, 2009

Automatically Get Images on Post Content

The problem. Using custom fields to display images associated with your post is definitely a great idea, but many WordPress users would like a solution for retrieving images embedded in the post’s content itself.

The solution. As far as we know, there’s no plug-in to do that. Happily, the following loop will do the job: it searches for images in post content and displays them on the screen.

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

Wordpress Adsense Shortcode

function showads() {  
 return '<div id="adsense"><script type="text/javascript"><!--  
 google_ad_client = "pub-XXXXXXXXXXXXXX";  
 google_ad_slot = "4668915978";  
 google_ad_width = 468;  
 google_ad_height = 60;  
 //-->  
 </script>  
 
 <script type="text/javascript"  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">  
 </script></div>';  
 }  
 
 add_shortcode('adsense', 'showads');

WordPress Bloginfo

<?php bloginfo('name'); ?>  Blog Title
<?php bloginfo('url'); ?>  Base URL
<?php bloginfo('template_directory'); ?> Current active template directory
<?php bloginfo('description'); ?>  Blog description or tagline
<?php bloginfo('admin_email'); ?> admin email address
<?php bloginfo('comments_atom_url'); ?>  comments atom feed
<?php bloginfo('comments_rss2_url'); ?> comments rss2 feed
<?php bloginfo('rdf_url'); ?>  rdf feed
<?php bloginfo('rss2_url'); ?>  rss2 feed
<?php bloginfo('rss_url'); ?>  rss feed
<?php bloginfo('siteurl'); ?>  Home Page
<?php bloginfo('stylesheet_directory'); ?>  style sheet directory
<?php bloginfo('stylesheet_url'); ?> style sheet url
<?php bloginfo('version'); ?>  2.8.5
<?php bloginfo('wpurl'); ?>  http://example/home/wp

Display Random Posts in Wordpress

<ul>
<?php
$rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php the_time('M d Y'); ?>
</li>
<?php endforeach; ?>
</a>
</ul>

Display Random Images from Media Gallery

<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'rand'
);
$attachments = get_posts($args);
$noimages = count($attachments);
 
if ($attachments) {
 
foreach ($attachments as $attachment) {
$alttxt = $attachment->post_title;
$imgid = $attachment->ID;
$fileurl = $attachment->guid;
 
$meta = wp_get_attachment_metadata($imgid);
$imgw = $meta['sizes']['thumbnail']['width'];
$imgh = $meta['sizes']['thumbnail']['height'];
 
$imgext = substr($fileurl, -4);
$fileurl = substr($fileurl, 0, -4);
$fileurl = $fileurl."-".$imgw."x".$imgh.$imgext;
 
// construct the image
echo "<img src='".$fileurl."' alt='".$alttxt."' class='alignleft highlightimg' />";
break;
}
}
the_excerpt('');
?>

Display Subpage Nav in Wordpress

<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>

Dynamic Title in Wordpress

If you’re not interested in changing your title tags on a post-to-post basis, then this is a good solution to show relevant titles to the page that the user is on.

<title>
<?php
if (is_home()) {
echo bloginfo('name');
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo 'Category:'; wp_title('');
} elseif (is_search()) {
echo 'Search Results';
} elseif ( is_day() || is_month() || is_year() ) {
echo 'Archives:'; wp_title('');
} else {
echo wp_title('');
}
?>
</title>