Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize taxonomy columns #11

Closed
seldimi opened this issue May 17, 2017 · 7 comments
Closed

Customize taxonomy columns #11

seldimi opened this issue May 17, 2017 · 7 comments

Comments

@seldimi
Copy link

@seldimi seldimi commented May 17, 2017

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
Copy link
Owner

@jjgrainger jjgrainger commented May 18, 2017

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
Copy link
Author

@seldimi seldimi commented May 18, 2017

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
Copy link
Owner

@jjgrainger jjgrainger commented May 18, 2017

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
Copy link
Author

@seldimi seldimi commented May 18, 2017

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
Copy link
Author

@seldimi seldimi commented May 18, 2017

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
Copy link
Owner

@jjgrainger jjgrainger commented May 18, 2017

👍

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
Copy link
Owner

@jjgrainger jjgrainger commented Oct 24, 2017

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants