Jump to content

Custom Post Type in WordPress: taxonomy + columns


sergi

Recommended Posts

In the WordPress video on Custom Post Types, Ben teaches us how to create a custom post type of Products. It's easy enough to have 2 custom post types by simply using add_action then register_post_type. However, I can't seem to replicate the other features he add to that custom post type: the inpu, the taxonomy and the columns.

 

this is the point at which things start to break:

 

function video_options()
{
global $post;
$custom = get_post_custom($post->ID);
$length = $custom['length'][0];

echo '<label style="padding-right:40px;">Length:</label>
	<input name="length" value="'. $length . '" style="width:250px;" />';
}

function mag_options()
global $post;
$custom = get_post_custom($post->ID);
$issue = $custom['issue'][0];

echo '<label style="padding-right:40px;">Issue:</label>
	<input name="issue" value="'. $issue . '" style="width:250px;" />';
}

 

this part comes next and I'm not sure how to handle it, either:

 

function save_options()
{
global $post;
if (!isset($_POST['length']) || $post->post_type != 'video')
{
	return $post;
}
update_post_meta($post->ID, "length", $_POST['length']);
}

 

I wonder if somebody can help me tackle this. Thanks much.

Link to comment
Share on other sites

Can you post the full functions.php file? That would make it a bit easier to place in at test theme and see what is currently working and what isn't. I have found that working with multiple custom post types is a bit of a pain. It takes a bit of trial and error, and (hopefully) a solid understanding of PHP itself.

Link to comment
Share on other sites

Can you post the full functions.php file? That would make it a bit easier to place in at test theme and see what is currently working and what isn't. I have found that working with multiple custom post types is a bit of a pain. It takes a bit of trial and error, and (hopefully) a solid understanding of PHP itself.

Ah! Well, worst comes to worst, I'll use Custom Post UI plugin. But I'm really interested in figuring out how to accomplish multiple post types. Appreciate any insight you have into this dirty work. :)


<?
/* create custom posts 
*** mags
*/
add_action('init', 'create_mags');
function create_mags()
{
$labels = array(
	'name' => __( 'Mags' ),
	'singular_name' => __( 'Mags' ),
	'add_new' => __( 'Add New' ),
	'add_new_item' => __( 'Add New Mag' ),
	'edit' => __( 'Edit' ),
	'edit_item' => __( 'Edit Mag' ),
	'new_item' => __( 'New Mag' ),
	'view' => __( 'View Mag' ),
	'view_item' => __( 'View Mag' ),
	'search_items' => __( 'Search Mags' ),
	'not_found' => __( 'No Mag Entries found' ),
	'not_found_in_trash' => __( 'No Mag Entries found in Trash' ),
	'parent' => __( 'Parent Mag' )
);

register_post_type(
	'mag',
	array(
		'labels' => $labels,
		'public' => true,
		'show_ui' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'rewrite' => array("slug" => "mags"),
		'supports' => array('title', 'editor', 'thumbnail')
	)
);
}

/* custom inputs */
add_action('admin_init', 'admin_init');
add_action('save_post', 'save_options');

function admin_init()
{
add_meta_box(
	"vidInfo-meta",
	"Video Options",
	"video_options",
	"video",
	"normal",
	"low"
);
}


function video_options()
{
global $post;
$custom = get_post_custom($post->ID);
$length = $custom['length'][0];

echo '<label style="padding-right:40px;">Length:</label>
	<input name="length" value="'. $length . '" style="width:250px;" />';
}


/*

			starting here, error

*/



function mag_options()
       global $post;
       $custom = get_post_custom($post->ID);
       $issue = $custom['issue'][0];

       echo '<label style="padding-right:40px;">Issue:</label>
               <input name="issue" value="'. $issue . '" style="width:250px;" />';
}




/*
			how to create save_options for both mag and video?
*/	

function save_options()
{
global $post;
if (!isset($_POST['length']) || $post->post_type != 'video')
{
	return $post;
}
update_post_meta($post->ID, "length", $_POST['length']);
}

/* create custom taxonomy */
add_action('init', 'build_taxonomy', 0);
function build_taxonomy()
{
// begin Vid
$labels = array(
    'name' => _x( 'Video Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Video Type', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Video Types' ),
    'all_items' => __( 'All Video Types' ),
    'parent_item' => __( 'Parent Video Type' ),
    'parent_item_colon' => __( 'Parent Video Type:' ),
    'edit_item' => __( 'Edit Video Type' ), 
    'update_item' => __( 'Update Video Type' ),
    'add_new_item' => __( 'Add New Video Type' ),
    'new_item_name' => __( 'New Video Type' ),
  );

register_taxonomy(
	'type',
	'video',
	array(
		'hierarchical' => true,
		'labels' => $labels,
		'query_var' => true,
		'rewrite' => true
	)
);



}

/* custom columns */
add_filter("manage_edit-video_columns", "edit_columns" );
add_action("manage_posts_custom_column", "custom_columns");

function edit_columns($columns)
{
$columns = array(
	"cb" => "<input type ='checkbox' />",
	"title" => "Video Title",
	"description" => "Description",
	"price" => "Length",
	"type" => "Video Type" 
);
return $columns;
}

function custom_columns($column)
{
global $post;
switch ($column)
{
	case "description":
		the_excerpt();
		break;
	case "length":
		$custom = get_post_custom($post->ID);
		echo $custom['length'][0];
		break;
	case "type":
		echo get_the_term_list($post->ID, 'type', '', ', ', '');
		break;
}

}


?>

Link to comment
Share on other sites

Well, the error you are getting is exactly what the error messages says:

 

Parse error: syntax error, unexpected T_GLOBAL, expecting '{' in /Applications/MAMP/htdocs/Wordpress/wp-content/themes/twentyten/functions.php on line 75

you are missing an opening "{" after "function mag_options()" near line 75.

Link to comment
Share on other sites

so glad to know about the missing "{".

 

This whole thing is bringing to light my confusion between CPTs (custom post types) and taxonomy. I've been considering them as inseparable. Am I correct?

 

This is my aim: I have a 2 main categories: Video and Magazine. Furthermore, Videos are either Fiction or Non-Fiction; and Magazines are either Comedy or Drama.

 

Should my code attempt the following: create a CPT for Movies w/ a taxonomy for Comedy and a tax. for Drama? And in addition, create a CPT for Videos, w/ a tax. for Fiction and a tax. for Non-Fiction?

 

Let's say the answer is "yes". How do I code that?

 

Thanks for your patience!!!!

 

Here is my stab at that code, but it breaks.

 


/* create custom posts 
*** vids
*/
add_action('init', 'create_vids');
function create_vids()
{
       $labels = array(
               'name' => __( 'Vids' ),
               'singular_name' => __( 'Vids' ),
               'add_new' => __( 'Add New' ),
               'add_new_item' => __( 'Add New Mag' ),
               'edit' => __( 'Edit' ),
               'edit_item' => __( 'Edit Mag' ),
               'new_item' => __( 'New Mag' ),
               'view' => __( 'View Mag' ),
               'view_item' => __( 'View Mag' ),
               'search_items' => __( 'Search Vids' ),
               'not_found' => __( 'No Vid Entries found' ),
               'not_found_in_trash' => __( 'No Vid Entries found in Trash' ),
               'parent' => __( 'Parent Vid' )
       );

       register_post_type(
               'vid',
               array(
                       'labels' => $labels,
                       'public' => true,
                       'show_ui' => true,
                       'capability_type' => 'post',
                       'hierarchical' => false,
                       'rewrite' => array("slug" => "vids"),
                       'supports' => array('title', 'editor', 'thumbnail')
               )
       );
}



/* create custom posts 
*** mags
*/
add_action('init', 'create_mags');
function create_mags()
{
       $labels = array(
               'name' => __( 'Mags' ),
               'singular_name' => __( 'Mags' ),
               'add_new' => __( 'Add New' ),
               'add_new_item' => __( 'Add New Mag' ),
               'edit' => __( 'Edit' ),
               'edit_item' => __( 'Edit Mag' ),
               'new_item' => __( 'New Mag' ),
               'view' => __( 'View Mag' ),
               'view_item' => __( 'View Mag' ),
               'search_items' => __( 'Search Mags' ),
               'not_found' => __( 'No Mag Entries found' ),
               'not_found_in_trash' => __( 'No Mag Entries found in Trash' ),
               'parent' => __( 'Parent Mag' )
       );

       register_post_type(
               'mag',
               array(
                       'labels' => $labels,
                       'public' => true,
                       'show_ui' => true,
                       'capability_type' => 'post',
                       'hierarchical' => false,
                       'rewrite' => array("slug" => "mags"),
                       'supports' => array('title', 'editor', 'thumbnail')
               )
       );
}

/* create custom taxonomy 
Vid
*/
add_action('init', 'build_taxonomy', 0);
function build_taxonomy()
{
       // begin Vid
       $labels = array(
           'name' => _x( 'Vid Types', 'taxonomy general name' ),
           'singular_name' => _x( 'Vid Type', 'taxonomy singular name' ),
           'search_items' =>  __( 'Search Vid Types' ),
           'all_items' => __( 'All Vid Types' ),
           'parent_item' => __( 'Parent Vid Type' ),
           'parent_item_colon' => __( 'Parent Vid Type:' ),
           'edit_item' => __( 'Edit Vid Type' ), 
           'update_item' => __( 'Update Vid Type' ),
           'add_new_item' => __( 'Add New Vid Type' ),
           'new_item_name' => __( 'New Vid Type' ),
         );

       register_taxonomy(
               'type',
               'vid',
               array(
                       'hierarchical' => true,
                       'labels' => $labels,
                       'query_var' => true,
                       'rewrite' => true
               )
       );



}

/* create custom taxonomy 
Mag
*/
add_action('init', 'build_taxonomy', 0);
function build_taxonomy()
{
       // begin Mag
       $labels = array(
           'name' => _x( 'Mag Types', 'taxonomy general name' ),
           'singular_name' => _x( 'Mag Type', 'taxonomy singular name' ),
           'search_items' =>  __( 'Search Mag Types' ),
           'all_items' => __( 'All Mag Types' ),
           'parent_item' => __( 'Parent Mag Type' ),
           'parent_item_colon' => __( 'Parent Mag Type:' ),
           'edit_item' => __( 'Edit Mag Type' ), 
           'update_item' => __( 'Update Mag Type' ),
           'add_new_item' => __( 'Add New Mag Type' ),
           'new_item_name' => __( 'New Mag Type' ),
         );

       register_taxonomy(
               'type',
               'mag',
               array(
                       'hierarchical' => true,
                       'labels' => $labels,
                       'query_var' => true,
                       'rewrite' => true
               )
       );



}

/* custom columns */
add_filter("manage_edit-video_columns", "edit_columns" );
add_action("manage_posts_custom_column", "custom_columns");

function edit_columns($columns)
{
       $columns = array(
               "cb" => "<input type ='checkbox' />",
               "title" => "Video Title",
               "description" => "Description",
               "price" => "Length",
               "type" => "Video Type" 
       );
       return $columns;
}

function custom_columns($column)
{
       global $post;
       switch ($column)
       {
               case "description":
                       the_excerpt();
                       break;
               case "length":
                       $custom = get_post_custom($post->ID);
                       echo $custom['length'][0];
                       break;
               case "type":
                       echo get_the_term_list($post->ID, 'type', '', ', ', '');
                       break;
       }

}

Link to comment
Share on other sites

No, custom post types and taxonomies are completely different. Taxonomies are ways to organize your post types. For example, categories and tags are both taxonomies that come preinstalled with Wordpress. To be honest, I don't think you need custom taxonomies at all -- you could use regular categories to organize those post types. If so, when creating those custom post types, you would need to add "category" to taxonomy[] array when creating the custom post type:

 

register_post_type(
               'vid',
               array(
                       'labels' => $labels,
                       'public' => true,
                       'show_ui' => true,
                       'capability_type' => 'post',
                       'hierarchical' => false,
                       'rewrite' => array("slug" => "vids"),
                       'supports' => array('title', 'editor', 'thumbnail'), <-- added comma
                       'taxonomies' => array('category') <-- added line
               )
       );

Also, if you want custom columns for your video type, you need to use the correct custom post type name:

 

add_filter("manage_edit-vid_columns", "edit_columns" );

 

not

 

add_filter("manage_edit-video_columns", "edit_columns" );

 

if you switch to using categories rather than a custom taxonomy, you'd need to update:

 

function custom_columns($column)
{
       global $post;
       switch ($column)
       {
               case "description":
                       the_excerpt();
                       break;
               case "length":
                       $custom = get_post_custom($post->ID);
                       echo $custom['length'][0];
                       break;
               case "type":
                       echo get_the_term_list($post->ID, 'category', '', ', ', ''); <-- updated
                       break;
       }

}

Hopefully this will get you moving in the right direction?

Link to comment
Share on other sites

×
×
  • Create New...