I recently needed some subpage code for a WordPress project. Darren Hoyt had a quick and easy solution to show the child pages of a parent page, only if the page had subpages. This was useful. My project needed to include the parent page at the top of the list of child pages, so I’ve added a small modification to it.

This goes in your page template or sidebar template where you want your submenu to appear. It is outside the loop.

<?php
 $parent_title = get_the_title($post->post_parent);
 if($post->post_parent)
 $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=0&depth=1"); else
 $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->ID."&echo=0&depth=1");
 if ($children && is_page()) { ?>
     <ul class="menu">
       <li <?php if ( is_page() && $post->post_parent ) {} else { ?>class="current_page_item"<?php } ?>><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li>
          <?php echo $children; ?>
     </ul>
 <?php } ?>

As it stands, the only issue with this code, is that when you are on the parent page, the class that identifies it as current does not appear. If you know of a solution for this, please do tell.

Updated Oct 7th, 2011 to include “current” state on parent page.

Read More

This will allow you to use Timthumb while showing all images within a WordPress post gallery. In other words, if I upload 4 photos to a post, they will automatically be shown wherever I insert this code in my template, within that post’s loop.

This is assuming that the timthumb script is in your theme’s directory: wp-content/themes/mytheme/timthumb.php

<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
	if ($attachments) {
	foreach ( $attachments as $attachment ) { ?>
      <img src="<?php bloginfo('template_url'); ?>/thumb.php?w=200&amp;h=200&amp;zc=1&amp;src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="<?php the_title(); ?>" width="200" height="200" border="0" />
<?php	}
	} ?>

To have each thumbnail link to the full sized original image, have the anchor href point to

<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>

Read More

Have you ever wondered how your WordPress posts might look when they’re shared on Facebook?

If you’ve ever shared a link on your Facebook account, you’ve noticed the nifty Title, Image and Description that get automatically loaded to provide an attractive link. Depending on how your particular theme is built, sometimes you’ll have to cycle through several images, before the actual appropriate post image shows up with your Facebook link.

Here is a bit of code you can add to the head section of the header.php file of your theme (Try adding it before the wp_head(); tag) to guarantee that your theme is optimized for Facebook link sharing. It will also make sure that the appropriate post image is the first one that Facebook looks for.

Code Last Updated June 3rd 2011

This update allows you to set the “Featured Image” as the the image that Facebook shows when your link is shared. Secondly, it allows you to set a default image, in case your post does not make use of a “featured image”. You can use a promo graphic, or your logo for this default, by saving it in your themes directory, in a /images/ directory with the file name as default_icon.jpg. Or you can alter the image directory in the code below

 
<!-- Begin FB Sharing for WP by Chad Von Lind. Get the latest code here: http://vonlind.com/?p=539  -->
<?php
	$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
	$thumb = wp_get_attachment_image_src($thumb[0], false);
	$thumb = $thumb[0];
	$default_img = get_bloginfo('stylesheet_directory').'/images/default_icon.jpg';
 
	?>
 
<?php if(is_single() || is_page()) { ?>
	<meta property="og:type" content="article" />
	<meta property="og:title" content="<?php single_post_title(''); ?>" />
	<meta property="og:description" content="<?php
	while(have_posts()):the_post();
	$out_excerpt = str_replace(array("\r\n", "\r", "\n"), "", get_the_excerpt());
	echo apply_filters('the_excerpt_rss', $out_excerpt);
	endwhile; 	?>" />
	<meta property="og:url" content="<?php the_permalink(); ?>"/>
	<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php  } else { ?>
	<meta property="og:type" content="article" />
   <meta property="og:title" content="<?php bloginfo('name'); ?>" />
	<meta property="og:url" content="<?php bloginfo('url'); ?>"/>
	<meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <meta property="og:image" content="<?php  if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php  }  ?>
<!-- End FB Sharing for WP -->

Be Aware: Updating your code will not have immediate effects with Facebook, as Facebook will cache your shared link. It could take up to a few hours before the shared link would work properly. You can try to share one of your links that has not been shared before, to test that it is working proper.

Coming soon: At the moment, if you don’t have an image set as “featured”, it will just default to your chosen default image in your theme directory. I’ll be adding the ability for it to check for the first image used in the post if there is no featured image. So the order will be 1) Featured, if not 2) First Image in post, if not 3) Use default image in theme directory.

Read More

If you’ve ever tried adding an iframe to your WordPress post, and then switched the TinyMCE editor to Visual mode, you’ve found that the editor strips out your iframe code. This can be annoying. Just add this snippet to the functions.php file in your theme folder to prevent TinyMCE from killing your iframe.

function add_iframe($initArray) {
$initArray['extended_valid_elements'] = "iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]";
return $initArray;
}
add_filter('tiny_mce_before_init', 'add_iframe');

Read More

Here’s a quick and easy way to update image sizes on the fly directly in your WordPress posts. Add this code into your functions.php file in your theme. Then make sure you insert timthumb.php in the root of your theme directory. Next create a fully writable directory named ‘cache’ and place that in your theme root directory.

Now inside your posts just wrap an image URL with the img shortcode. Add height, width, alt, and class to it for more flexibility.

7/15/2011 – Updated to allow use within a text widget.

// Image resize in content
// To Use: {img w=600 h=200 class=alignleft alt=My Photo}http://mysite.com/photo.jpg{/img}
function timmyimg($atts, $content = null) {
	extract(shortcode_atts(array(
		'w' => '600',
		'h' => '250',
		'class' => 'alignnone',
		'alt' => ''
	), $atts));
	return "<img src='". get_bloginfo('template_directory') . "/timthumb.php?src=".$content."&w=".$w."&h=".$h."' alt='".$alt."' class='wp-post-img ".$class."' />";
}
add_shortcode('img', 'timmyimg');
 
// Allows this shortcode to work within a text widget
add_filter('widget_text', 'do_shortcode');

Forest

Forest

Forest

Read More

Occasionally you’ll need to fetch a posts image outside of the loop.

<?php
    $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
    $thumb = wp_get_attachment_image_src($thumb[0], false);
    $thumb = $thumb[0];?>
 
   <img src="<?php  echo $thumb; ?>" alt="" />

Combine this with the Timthumb script for control over image size. IE:

 
<?php
   $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
   $thumb = wp_get_attachment_image_src($thumb[0], false);
   $thumb = $thumb[0];
  if ( has_post_thumbnail() ) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $thumb; ?>&amp;w=150&amp;h=150&amp;zc=1"
        alt="<?php the_title(); ?>" width="150" height="150" />
<?php } ?>

Not Recommended, But Available Option

This will output the URL to an image with the size predefined by ‘post-thumbnail’ in your functions file when registering the functionality for post images.

<?php
    $thumb = get_post_meta($post->ID,'_thumbnail_id',false);
    $thumb = wp_get_attachment_image_src($thumb[0], 'my-custom-size', false);
    $thumb = $thumb[0];
    echo $thumb; ?>

The above code is referencing ‘my-custom-size’, which we can see below as being 405×180. Typically it is not advised to use these predefined sizes as it will overload your uploads directory with additional sizes, and it puts strain on your server when uploading images. Better to use the above method with the Timthumb script to get an exact image size, while making use of image caching.

if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 50, 50, true ); // Normal post thumbnails
	add_image_size( 'my-custom-size', 405, 180, true );
}

Read More
Design Color Scheme From A Photograph

As a designer I find inspiration for color schemes from all over. From architecture, nature, to paintings and photographs. I’ve driven by houses with a great color scheme and thought to myself “That would make a great color scheme for a website!”.

Using Photoshop’s Filter > Pixelate > Mosaic, we can grab a photograph and find a great scheme from it! Hover the below photos to see the scheme they produce at 65 cell size. Photographs with a wide array of colors will work best.

Read More
Embed Facebook Fan Page Photo Albums In WordPress

It is a great idea to have a Facebook fan page for your company, club or organization.

This is a minor hack of the Fotobook plugin (Version 3.1.8) for WordPress by Aaron Harp. Fotobook is originally meant to apply your personal photo albums into WordPress, but we’re going to make one small code tweak to instead pull the albums from the fan page that we administer.

After installing the plugin, go ahead and activate it and grant the permissions to your Facebook account.

Next, visit your Facebook fan page, and click into an album so you’re viewing all photos in one particular album. In the address bar you’ll see a &id=123123123. Copy that ID number.

In the plugin directory open the fotobook.php file and head down to line 243 and look for this code.

$uid = $session['uid'];

Swap that code for this

//$uid = $session['uid'];
$uid = 123123123;

With the 123123123 being the ID code for your fan page.

If you have a custom URL set for your fan page you might notice that the album photos are not showing. To correct that move down around line 809 in fotobook.php and look for:

if(!is_numeric($id)) return false;

And code it out of use by adding // in front so that it now looks like

//if(!is_numeric($id)) return false;

Back in the plugin administration page, go ahead and import your albums. And you should be good to go!

With a thanks to B. Thibault in the forums at the Fotobook Application Page.

Read More

Advertising may be the only business in the world where the clients with the most money can make demands until they get the agency’s worst product, while the small client with little to spend must meekly accept the agency’s best.
Thomas Murray

Read More

I recently noticed that a particular post had built up about 10 or 15 revisions for it. I started thinking that of all the posts I have, there have to be quite a few revisions and therefore are creating a hefty sized table in my database. This was run on my WP 2.8, but should work for 2.6 and up.

Here is a solution to prevent revisions from being created, as well as removing all revisions from your database.

Prevent Revision Creation

Open wp-config.php and add this to the very bottom of the file, before the php closing tag.

define('WP_POST_REVISIONS', false);

This completely turns off any revision creation. If you’d like to restrict revision creation to a set amount per post, you can change ‘false’ for the number of revisions you’d like stored.

Remove All Revisions From Database

Open up your database in phpmyadmin and run this SQL Query.

DELETE FROM wp_posts WHERE post_type = "revision";

And now you’ll have a more tidy database.

Read More