Add Thumbnails in Manage Posts/Pages and Other Post Types

Hi there, here we go again! For today I’ve prepared you this simple ready to copy functions to display to the Manage/Edit Post and Pages and my custom taxonomy Downloads a new column with the thumbnail preview.
Thumbnails in Manage Posts

This snippet will first search for thumbnail and it’s id, but if fail then look for attachment used in the image there are no thumbnails on the posts to call a image[0], or the first image in the post, page or download type.

The default thumb size is 55px x 55px but you can easy change it on this snippet code. Simply you just have to edit $width and $height values. The same worth for custom post type selection, just edit this part of a code add_theme_support(‘post-thumbnails’, array( ‘post‘, ‘page‘, ‘download‘ ) );.

Add Thumbnails in Manage Posts/Pages

[php]
/****** Add Thumbnails in Manage Posts/Pages List ******/
/**
* Snippet Name: Add Thumbnails in Manage Posts/Pages and Other Post Types
* Snippet URL: http://wperror.com/add-thumbnails-in-manage-postspages-and-other-post-types.html
*/
if ( !function_exists (‘AddThumbColumn’) && function_exists (‘add_theme_support’) ) {

// for post and page
add_theme_support (‘post-thumbnails’, array( ‘post’, ‘page’ ) );

function AddThumbColumn ($cols) {

$cols[‘thumbnail’] = __(‘Thumbnail’);

return $cols;
}

function AddThumbValue ($column_name, $post_id) {

$width = (int) 35;
$height = (int) 35;

if ( ‘thumbnail’ == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, ‘_thumbnail_id’, true );
// image from gallery
$attachments = get_children( array(‘post_parent’ => $post_id, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’) );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __(‘None’);
}
}
}

// for posts
add_filter( ‘manage_posts_columns’, ‘AddThumbColumn’ );
add_action( ‘manage_posts_custom_column’, ‘AddThumbValue’, 10, 2 );

// for pages
add_filter( ‘manage_pages_columns’, ‘AddThumbColumn’ );
add_action( ‘manage_pages_custom_column’, ‘AddThumbValue’, 10, 2 );
}
[/php]

Add custom post types to archives page

CPT ArchivesAfter we have added custom post types snippet into our functions.php we expect that our new articles under the custom post type to appear on archive pages, but they will not. It’s very logical from the developers side, WordPress maintain that sorting archives is for normal post types “posts”, and adding custom post types into categories and post_tags requires some custom code.

So, if you’d like your custom post types to show up in archive listings for your site’s standard tags and categories you can add the following code to your functions.php file:

Add custom post types to archives

[php]
/**
* Snippet Name: Add custom post types to archives page
* Snippet URL: http://wperror.com/add-custom-post-types-to-archives-page.html
*/

function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {

// Get all your post types
$post_types = get_post_types();

$query->set( ‘post_type’, $post_types );
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );
[/php]
Still if you wish to display only specific custom post types into archive pages of tags and categories you can replace the line $post_types = get_post_types(); with $post_types = array( ‘post’, ‘your_custom_type’ );.

Create a Custom Post Type Download

Custom Post Type Download

Nowadays when WordPress become most used CMS, users are searching for various improvements for their sites. WordPress have ability to add custom post types and taxonomies easily with some snippet code in your functions.php file, or even easier trough the various plugins witch brings you this and similar options like custom taxonomies, custom meta boxes, fields etc. All those snippets/add-ons can quickly turn your WordPress site into well-featured CMS.

One of the easiest ways to add custom post type is trough the free WordPress plugin Custom Post Type Maker. Personally I preffer adding them with snippet code so I brings you simple snippet code that is ready to paste into functions.php file.

Custom Post Type Snippet

[php]
/**
* Snippet Name: Create a Custom Post Type "Download"
* Snippet URL: http://wperror.com/create-a-custom-post-type-download.html
*/

add_action( ‘init’, ‘register_cpt_download’ );
function register_cpt_download() {
$labels = array(
‘name’ => _x( ‘Downloads’, ‘download’ ),
‘singular_name’ => _x( ‘Download’, ‘download’ ),
‘add_new’ => _x( ‘Add New’, ‘download’ ),
‘add_new_item’ => _x( ‘Add New Download’, ‘download’ ),
‘edit_item’ => _x( ‘Edit Download’, ‘download’ ),
‘new_item’ => _x( ‘New Download’, ‘download’ ),
‘view_item’ => _x( ‘View Download’, ‘download’ ),
‘search_items’ => _x( ‘Search Downloads’, ‘download’ ),
‘not_found’ => _x( ‘No downloads found’, ‘download’ ),
‘not_found_in_trash’ => _x( ‘No downloads found in Trash’, ‘download’ ),
‘parent_item_colon’ => _x( ‘Parent Download:’, ‘download’ ),
‘menu_name’ => _x( ‘Downloads’, ‘download’ ),
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘trackbacks’, ‘custom-fields’, ‘comments’, ‘revisions’, ‘page-attributes’ ),
‘taxonomies’ => array( ‘category’, ‘post_tag’ ),
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 5,
‘menu_icon’ => ”,
‘show_in_nav_menus’ => true,
‘publicly_queryable’ => true,
‘exclude_from_search’ => false,
‘has_archive’ => true,
‘query_var’ => true,
‘can_export’ => true,
‘rewrite’ => true,
‘capability_type’ => ‘post’
);
register_post_type( ‘download’, $args );
}
[/php]

The known issue when the custom post types are created trough the snippet is that WordPress does not automatically take your custom post types and add them to the archive pages for existing taxonomies like categories or post_tags. Because of that we have prepared you another snippet for adding custom post types to archives page with snippet too.