jjgrainger / PostTypes

Simple WordPress custom post types.
https://posttypes.jjgrainger.co.uk/
MIT License
373 stars 47 forks source link

Customize taxonomy columns #11

Closed seldimi closed 6 years ago

seldimi commented 7 years ago

First of all, I love your class, I just updated from CTP!

Is there any way to customize taxonomy columns, as we do with custom post type? That would be handy

jjgrainger commented 7 years ago

Hi @seldimi

If you're referring to the post types admin columns, you can use the columns method to add taxonomy columns.

If you need any extra help, just give me some extra details and code examples if possible and I'll do my best.

Thanks :)

seldimi commented 7 years ago

Hello @jjgrainger Let's take this for example. $books->taxonomy('genre');

I want to set some columns on Genre (taxonomy term table) which refer to taxonomy custom field, not in Custom Post Type table

jjgrainger commented 7 years ago

Hi @seldimi

Sorry unfortunately not. In the meantime, you can set it up yourself with a couple of actions.

To add the custom column to the genre taxonomy table.

function add_genre_columns($columns) {
    // Add your custom column
    $columns['my_columns'] = 'My Column';

    return $columns;
}

add_filter('manage_edit-genre_columns', 'add_genre_columns');

Then to populate the column.

function populate_genre_column($content, $column_name, $term_id) {
    // get the term object by the id
    $term= get_term($term_id, 'genre');

    switch ($column_name) {
        case 'my_column':
            // grab custom field using the $term or $term_id
            $content = 'custom field content';
            break;
        default:
            break;
    }

    return $content;
}

add_filter('manage_genre_custom_column', 'populate_genre_column', 10, 3);

You can see an example here on stackoverflow

seldimi commented 7 years ago

Thanks for the help. It would really help many people out, someting like $books->taxonomy('genre')->columns() and can be used the same as $book->columns()

seldimi commented 7 years ago

I leave that code here in case someone seeks for it.. Its cleaner and can ovveride all columns, as you do on your Class

function add_genre_columns($columns) {
    $new_columns = array(
        'cb' => '<input type="checkbox" />',
        'name' => __('Name'),
        'slug' => __('Slug'),
        'posts' => __('Books'),
        'my_column' => __('My Column')
        );
    return $new_columns;
}
jjgrainger commented 7 years ago

👍

Yes, I'll look into adding it as a feature... something to consider while looking at some of the refactoring work I'm considering here #9

Thanks :)

jjgrainger commented 6 years ago

Hi @seldimi

v2.0 now has a Taxonomy class for creating taxonomies. This allows you to use the columns() method as you would with the PostType class, heres an example:


// Create a new Taxonomy
$genres = new Taxonomy('genre');

// Add a custom column to the taxonomy table
$genres->columns()->add([
    'my_column' => __('My Column')
]);

// Populate the custom column with term meta
$genres->columns()->populate('my_column', function($content, $column, $term_id) {
    return get_term_meta($term_id, 'my_custom_term_field', true);
});

// Register the taxonomy to WordPress
$genres->register();