Skip to content

Instantly share code, notes, and snippets.

@ksemel
Created May 19, 2014 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksemel/5afcb7c8d6a25df6e884 to your computer and use it in GitHub Desktop.
Save ksemel/5afcb7c8d6a25df6e884 to your computer and use it in GitHub Desktop.
Basic non-class functions.php set up
<?php
function Init_My_Theme() {
// Declare all your add_action and add_filter here so they all run when you call the init function
// Add the custom taxonomies
add_action( 'init', 'My_Theme_register_custom_taxonomy', 1 );
// On the posts page, add a column
add_filter( 'manage_posts_columns', 'My_Theme_add_taxonomy_column', 10, 1 );
add_action( 'manage_posts_custom_column', 'My_Theme_manage_taxonomy_column', 10, 2 );
}
// Add all the functions you hooked above below here
function My_Theme_register_custom_taxonomy() {
$taxonomy_name = 'custom-taxonomy';
$taxonomy_labels = array(
'name' => __( 'Custom Taxonomies' ),
'singular_name' => __( 'Custom' ),
'all_items' => __( 'All Custom Taxonomies' ),
'edit_item' => __( 'Edit Custom Taxonomy' ),
'update_item' => __( 'Update Custom Taxonomy' ),
'add_new_item' => __( 'Add New Custom Taxonomy' ),
'new_item_name' => __( 'New Custom Taxonomy' ),
'menu_name' => __( 'Custom Taxonomies' )
);
register_taxonomy(
$taxonomy_name,
array( 'post' ),
array(
'hierarchical' => false,
'labels' => $taxonomy_labels,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array(
'slug' => "customs",
'feeds' => true,
'with_front' => false,
),
'has_archive' => false
)
);
}
function My_Theme_add_taxonomy_column( $columns ) {
$columns['custom-taxonomy'] = _x( 'Custom Taxonomy', 'column name' );
return $columns;
}
function My_Theme_manage_taxonomy_column( $column_name, $post_id ) {
if ( $column_name != 'custom-taxonomy' ) {
return;
}
$post = get_post( $post_id );
$categories = get_the_terms( $post_id, 'custom-taxonomy' );
if ( !empty( $categories ) ) {
$out = array();
foreach ( $categories as $category ) {
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'custom-taxonomy' => $category->slug ), 'edit.php' ) ),
esc_html( sanitize_term_field( 'name', $category->name, $category->term_id, 'custom-taxonomy', 'display' ) )
);
}
echo join( ', ', $out );
} else {
_e( 'No Custom Taxonomy' );
}
}
// Start running everything here:
Init_My_Theme();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment