Hi Guys,
Hope you all doing good.
So here you'll found a wordpress function to create Custom Post Type with Taxonomies
/* Radio Post Type Section Start*/
add_action( 'init', 'radio' );
function radio() {
$labels = array(
'name' => _x('Radio Interviews', 'post type general name'),
'add_new' => 'Add New Interview',
'add_new_item' => 'Add New Interview',
'edit_item' => __('Edit Interview'),
'new_item' => __('New Interview'),
'all_items' => __('All Interviews'),
'view_item' => __('View Interviews'),
'search_items' => __('Search Interview'),
'not_found' => __('Empty'),
'not_found_in_trash'=> __('Empty Trash'),
'parent_item_colon' => '',
'menu_name' => __('Radio Interviews')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable'=> true,
'exclude_from_search'=> true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => NULL,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
);
register_post_type("radio_interviews",$args);
}
function programs_console_taxonomies() {
register_taxonomy('programs-console', 'radio_interviews', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Programs Category', 'taxonomy general name' ),
'singular_name' => _x( 'Programs-Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Programs-Categories' ),
'all_items' => __( 'All Programs-Categories' ),
'parent_item' => __( 'Parent Programs-Category' ),
'parent_item_colon' => __( 'Parent Programs-Category:' ),
'edit_item' => __( 'Edit Programs-Category' ),
'update_item' => __( 'Update Programs-Category' ),
'add_new_item' => __( 'Add New Programs-Category' ),
'new_item_name' => __( 'New Programs-Category Name' ),
'menu_name' => __( 'Programs Categories' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'programs-console', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => false // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'programs_console_taxonomies', 0 );
add_filter( 'manage_edit-programs_columns', 'my_edit_programs_columns' ) ;
function my_edit_programs_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Movie' ),
'duration' => __( 'Duration' ),
'genre' => __( 'Genre' ),
'date' => __( 'Date' )
);
return $columns;
}
add_action( 'manage_programs_posts_custom_column', 'my_manage_programs_columns', 10, 2 );
function my_manage_programs_columns( $column, $post_id ) {
global $post;
switch( $column ) {
/* If displaying the 'genre' column. */
case 'genre' :
/* Get the genres for the post. */
$terms = get_the_terms( $post_id, 'programs-console' );
/* If terms were found. */
if ( !empty( $terms ) ) {
$out = array();
/* Loop through each term, linking to the 'edit posts' page for the specific term. */
foreach ( $terms as $term ) {
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'programs' => $term->slug ), 'edit.php' ) ),
esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'programs-console', 'display' ) )
);
}
/* Join the terms, separating them with a comma. */
echo join( ', ', $out );
}
/* If no terms were found, output a default message. */
else {
_e( 'No Genres' );
}
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
Hope you all doing good.
So here you'll found a wordpress function to create Custom Post Type with Taxonomies
/* Radio Post Type Section Start*/
add_action( 'init', 'radio' );
function radio() {
$labels = array(
'name' => _x('Radio Interviews', 'post type general name'),
'add_new' => 'Add New Interview',
'add_new_item' => 'Add New Interview',
'edit_item' => __('Edit Interview'),
'new_item' => __('New Interview'),
'all_items' => __('All Interviews'),
'view_item' => __('View Interviews'),
'search_items' => __('Search Interview'),
'not_found' => __('Empty'),
'not_found_in_trash'=> __('Empty Trash'),
'parent_item_colon' => '',
'menu_name' => __('Radio Interviews')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable'=> true,
'exclude_from_search'=> true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => NULL,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
);
register_post_type("radio_interviews",$args);
}
/*Custom Taxonomies For Radio Interview Post Type*/
function programs_console_taxonomies() {
register_taxonomy('programs-console', 'radio_interviews', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Programs Category', 'taxonomy general name' ),
'singular_name' => _x( 'Programs-Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Programs-Categories' ),
'all_items' => __( 'All Programs-Categories' ),
'parent_item' => __( 'Parent Programs-Category' ),
'parent_item_colon' => __( 'Parent Programs-Category:' ),
'edit_item' => __( 'Edit Programs-Category' ),
'update_item' => __( 'Update Programs-Category' ),
'add_new_item' => __( 'Add New Programs-Category' ),
'new_item_name' => __( 'New Programs-Category Name' ),
'menu_name' => __( 'Programs Categories' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'programs-console', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => false // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'programs_console_taxonomies', 0 );
add_filter( 'manage_edit-programs_columns', 'my_edit_programs_columns' ) ;
function my_edit_programs_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Movie' ),
'duration' => __( 'Duration' ),
'genre' => __( 'Genre' ),
'date' => __( 'Date' )
);
return $columns;
}
add_action( 'manage_programs_posts_custom_column', 'my_manage_programs_columns', 10, 2 );
function my_manage_programs_columns( $column, $post_id ) {
global $post;
switch( $column ) {
/* If displaying the 'genre' column. */
case 'genre' :
/* Get the genres for the post. */
$terms = get_the_terms( $post_id, 'programs-console' );
/* If terms were found. */
if ( !empty( $terms ) ) {
$out = array();
/* Loop through each term, linking to the 'edit posts' page for the specific term. */
foreach ( $terms as $term ) {
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'programs' => $term->slug ), 'edit.php' ) ),
esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'programs-console', 'display' ) )
);
}
/* Join the terms, separating them with a comma. */
echo join( ', ', $out );
}
/* If no terms were found, output a default message. */
else {
_e( 'No Genres' );
}
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}

No comments:
Post a Comment