about those taxonomies in 2.8

Yes, another WP post, mostly for posterity, but you never know who else might find it handy.

The problem with the new custom taxonomies is that they’re not added automatically to the Edit-posts page, which I find annoying. So, I added them myself. Just cut & paste into your theme’s functions.php file (or create one if your theme doesn’t have one, which is possible if you don’t have a widgetized theme), and then change to suit your taxonomy needs.

/*----------------------------------------------
MANAGE COLUMNS & INC CH/PART NUMBERS
----------------------------------------------*/

add_action( 'init', 'create_my_taxonomies', 0 );

Here’s where you can insert new taxonomies: copy, paste, and replace square brackets + all-caps with new taxonomy name & label:
register_taxonomy( ‘[NAME]‘, ‘post’, array( ‘hierarchical’ => false, ‘label’ => ‘[LABEL FOR TAXONOMY]‘, ‘query_var’ => true, ‘rewrite’ => true ) );

I left in several of the taxonomies I did, as examples.

function create_my_taxonomies() {
register_taxonomy( 'note', 'post', array( 'hierarchical' => false, 'label' => 'Themes: Mild', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'warn', 'post', array( 'hierarchical' => false, 'label' => 'Themes: Strong', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'book', 'post', array( 'hierarchical' => false, 'label' => 'Books', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'series', 'post', array( 'hierarchical' => false, 'label' => 'Series', 'query_var' => true, 'rewrite' => true ) );
}

function notes_columns( $defaults ) {
$defaults['notes'] = __('Mild');
return $defaults;
}

function notes_column($column_name) {
global $post;
if( $column_name == 'notes' ) {
echo get_the_term_list( $post->ID, 'note', '', ', ', '' );
}
}

function warns_columns( $defaults ) {
$defaults['warns'] = __('Strong');
return $defaults;
}

function warns_column($column_name) {
global $post;
if( $column_name == 'warns' ) {
echo get_the_term_list( $post->ID, 'warn', '', ', ', '' );
}
}

If you’ve got numbered taxonomies (where sequential information is important), such as in CMS, then here are three examples of how to get those chapter or part numbers in there. They’re using functions I created elsewhere, like book_id() and section_count(); if anyone really needs those, just comment and I’ll add those in.

function books_columns( $defaults ) {
$defaults['books'] = __('Books');
return $defaults;
}

function books_column($column_name, $id) {
global $post, $wpdb;
if( $column_name == 'books' ) {
echo get_the_term_list( $post->ID, 'book', '', ', ', '' );
if (has_term('book')) {
$book = $post->post_title;
$cluster = get_term_by('name', $title, 'book');
$bookid = $cluster->term_id;
$chno = get_post_meta($id, 'chno', TRUE);
$total = get_terms('book', 'name__like=$title');
$booknum = section_count('book');
$column_content = '<br>ch'.$chno . ' of ' . $booknum;
echo  $column_content;
}
}
}

function series_columns( $defaults ) {
$defaults['series'] = __('Series');
return $defaults;
}

function series_column($column_name) {
global $post;
if( $column_name == 'series' ) {
echo get_the_term_list( $post->ID, 'series', '', ', ', '' );
$sername = get_the_term_list( $post->ID, 'series', '', ', ', '' );
if (has_term('series')) {
$serid = series_ID();
$part = get_post_meta($post->ID, 'part', TRUE);
$total = get_terms('series', 'name__like=$sername');
$sernum = section_count('series');
$column_content = '<br>pt'.$part . ' of ' . $sernum;
echo  $column_content;
}
}
}

Do a set of each of the following for each taxonomy you’ve added.

add_filter('manage_posts_columns', 'notes_columns');
add_action('manage_posts_custom_column', 'notes_column', 10, 2);
add_filter('manage_posts_columns', 'warns_columns');
add_action('manage_posts_custom_column', 'warns_column', 10, 2);
add_filter('manage_posts_columns', 'books_columns');
add_action('manage_posts_custom_column', 'books_column', 10, 2);
add_filter('manage_posts_columns', 'series_columns');
add_action('manage_posts_custom_column', 'series_column', 10, 2);

This last part is to make the columns behave. If you leave them as-is, the title, author, category, and tags get all this honking room while the taxonomies you’ve added will be squeezed over into tiny columns. Sure, you can turn columns off now via the screen options, but it just makes the native columns that much wider, and the added taxonomies stay the same tiny. So, a slight adjustment to the base CSS is needed.

function custom_cols_css() {
?>
<style type="text/css">
table.fixed {table-layout: fixed;}
.fixed .column-parent, .fixed .column-visible, .fixed .column-links  »
{width: 10%;}
.fixed .column-response, .fixed .column-rel, .fixed .column-role  »
{width: 15%;}
.fixed .column-categories, .fixed .column-tags {width: 10%;}
.fixed .column-date, .fixed .column-author {width: 8%;}
.fixed .column-comments {width: 4em;}
.fixed .column-slug {width: 25%;}
.fixed .column-posts {width: 10%;}
.fixed .column-icon {width: 80px;}
#commentstatusdiv .fixed .column-author, #comments-form .fixed »
.column-author, #commentstatusdiv .fixed   »
.column-date {width: 20%;}
</style>
<?php
}

add_action('admin_head', 'custom_cols_css');

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>