Captin of Zen

WordPress Pagination 404′s with Custom Post Types

If you’re using custom post types in WordPress and your category pages are throwing 404′s every time you try to paginate, you may be running into a bug with the WordPress core (as of version 3.0.4).

There is a solution — but first make sure that you have:

1) Checked your permalink settings to make sure there is nothing wonky. Try the recommended presets in the permalinks settings page and see if you are still having a problem.

2) View the same category page on the default skin. This will rule out issues that may lie within your specific code.

3) Cried a bit. Just kidding… kinda (I suffered for days before coming to a resolution courtesy of Mark / t31os)

If you’re still getting 404′s… here’s the code that saved my life. Replace your_custom_type with your custom type (surprise). You can put however many custom types as you’d like. Do this in your functions.php.

[php]add_action( 'parse_query','changept' );
function changept() {
	if( is_category() && !is_admin() )
		set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
	return;
}[/php]

Best of luck! You can view my entire sob story / thread here (and give Mark all of your money if possible).

How to Dynamically Create WordPress Posts

If you’ve ever had a need to dynamically create (or update) WordPress posts, there’s a simple way to do so.  Use wp_insert_post.

// Create post object
  $my_post = array(
     'post_title' => 'My post',
     'post_content' => 'This is my post.',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array(8,39)
  );

// Insert the post into the database
  wp_insert_post( $my_post );


If you set the ID property of your post array to a number  (i.e. $my_post['ID']  => 15), it will update an existing post rather than adding a new one. Leaving ID blank will create a new post. You cannot force a new post to acquire a specific ID.

For any further details, visit wp_insert_post in the WordPress Codex. For whatever reason, this documentation is not easy to find on Google, so I hope this simple article will make it easier for everyone.

Enhance #3: A Tribute to Salvador

WordPress: Automatically Generate Thumbnails from Your Post Images

WordPress automatically generates thumbnails for every image you upload. However, it does not make these thumbnails obviously accessible through code, and people have built a number of plugins to allow you to work around this.

Grabbing thumbnails and resized images is essential to creating galleries or grids of similar images. Knowing a few tricks about WordPress will help you do this more efficiently and without causing conflicts with other sections of your site.

1) Custom thumbnail sizes. You can have WordPress generate any number of thumbnails from every image you upload. For example, put the following code in functions.php.

if ( function_exists( ‘add_image_size’ ) ) {

add_image_size( ‘press-thumb’, 226, 292, true );
add_image_size( ‘news-thumb-small’, 63, 42, true );
add_image_size( ‘news-thumb-medium’, 228, 146, true );
add_image_size( ‘news-thumb-large’, 450, 230, true );
add_image_size( ‘news-thumb-xl’, 640, 390, true );

}

Note that creating a new image size requires WordPress to regenerate your thumbnails. Otherwise you will have odd sizes scaled to whatever you’ve recently specified. There is a handy plugin that can help you out with that.

Regenerate Thumbnails: http://wordpress.org/extend/plugins/regenerate-thumbnails/

2. wp_get_attachment_link

Every time you upload or insert images into a post, they will appear in the post’s attachments (or Gallery tab in the Upload/Insert pop-up). You can grab all of these images and select a thumbnail size using a combination of get_posts and wp_get_attachment_link.

$args = array(
‘post_type’ => ‘attachment’,
‘numberposts’ => -1,
‘post_status’ => NULL,
‘post_parent’ => $post->ID,
‘exclude’ => get_post_thumbnail_id()
);

$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, ‘media-thumb’);
}
}

In this code we simply echo’d the attachment, but by storing it in a variable instead you can just imagine how it’d be simple to reorganize this into a gallery or image grid.

Enhance! Gallery #2

100% Content-Aware Fill Tool (no other edits allowed)

Preventing Duplicate Facebook Comments in WordPress or Other Dynamic Pages

Have you tried using Facebook Comments on your WordPress or dynamic website only to find that every comment is the same?

The solution is simple. In your embed code, make sure that the XID is set to a unique ID, blank, or “”. When this is filled in, Facebook uses it to identify your specific comment box in case you want to place it in multiple locations and maintain the same comments. If you leave it blank, Facebook will use the URL of the page as the box’s unique identifier, meaning that every page will get a different comment box.

<div id=”fb-root”></div><script src=”http://connect.facebook.net/en_US/all.js#appId=0000000000000&amp;amp;xfbml=1″></script>
<fb:comments xid=”” numposts=”10″ width=”640″ publish_feed=”true”></fb:comments>

Enhance! Gallery 1

100% Content-Aware Fill Tool (no other edits allowed)