Categories
Responsive Web Design Wordpress

Claudio Bez

This is an italian web site which I built for Claudio Bez. He’s an artist who does cartoons, paintings, caricatures and illustrations, often combined with strong black and white draws.

You can check out the actual site if you want to see this design layout in action.

responsive-design-bez

bez-desktop-laptop-phone

Bez.desktop

http://bez.bz

Categories
Security Wordpress

How to show a custom post type in a category page in WordPress

If you want to use the default taxonomy category in a custom post type (i.e. products) it is easy, just create your new post type “Product” and link it to use the taxonomy category.

Example
add to your theme function.php the following:

add_action('init', 'create_my_post_types');
function create_my_post_types() {
	register_post_type(
		'products',
		array(
			'label' => 'Products',
			'description' => '',
			'public' => true,
			'show_ui' => true,
			'show_in_menu' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'rewrite' => array('slug' => '', 'with_front' => true),
			'query_var' => true,
			'supports' => array(
				'title',
				'editor',
				'excerpt',
				'trackbacks',
				'custom-fields',
				'comments',
				'revisions',
				'thumbnail',
				'author',
				'page-attributes'
			),
			'taxonomies' => array('category'),
			'labels' => array (
				'name' => 'Products',
				'singular_name' => 'Product',
				'menu_name' => 'Products',
				'add_new' => 'Add Product',
				'add_new_item' => 'Add New Product',
				'edit' => 'Edit',
				'edit_item' => 'Edit Product',
				'new_item' => 'New Product',
				'view' => 'View Product',
				'view_item' => 'View Product',
				'search_items' => 'Search Products',
				'not_found' => 'No Products Found',
				'not_found_in_trash' => 'No Products Found in Trash',
				'parent' => 'Parent Product'
			)
		)
	);
}

As alternative to create you post type you can easily use a WordPress plugin Custom Post Type UI or similar plugins.

Soon you will realise that when you go in a category page just default post will be displayed. Where are my products gone?

Strangely if you use the widget Categories with the tick “Show post counts” on you will see that the total number of post in a single category will be the sum of the default posts + the products (custom post type)

In order to have in the category page both the post types together you need to patch a bit the query that return the post inside a category.

Add to your theme function.php the following:

add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query) {
    if(is_category() && false == $query->query_vars['suppress_filters'])
        $query->set('post_type', array('post', 'products'));
    return $query;
}