admin管理员组文章数量:1293367
I'm working with a Taxonomy where the Terms have lengthy descriptions. It makes the Admin Panel look really ugly when WordPress tries to display these descriptions for 10 categories. I'm trying to figure out a way to limit the number of words or characters displayed in the Category / Term Description Column but I cannot find the correct hook to get to the category description as it's displaying it. I'm using this list of Hooks as a guide, and the two hooks that seem to point to the right direction are:
filter: manage_taxonomies_for_{$post_type}_columns
function funcy_test($empty = '', $column, $term_id)
{
return '';
}
add_filter('manage_taxonomies_for_cpt_products_columns', 'funcy_test', 10, 3);
I expected it to return empty columns all around but instead I didn't see a result. One possible solution is to remove the Description cateogry entirely and use the custom_columns
hook to add my own description column but that just seems too much of a hassle.
Is there a way to limit the words / characters for a term description in the Admin Panel?
I'm working with a Taxonomy where the Terms have lengthy descriptions. It makes the Admin Panel look really ugly when WordPress tries to display these descriptions for 10 categories. I'm trying to figure out a way to limit the number of words or characters displayed in the Category / Term Description Column but I cannot find the correct hook to get to the category description as it's displaying it. I'm using this list of Hooks as a guide, and the two hooks that seem to point to the right direction are:
filter: manage_taxonomies_for_{$post_type}_columns
function funcy_test($empty = '', $column, $term_id)
{
return '';
}
add_filter('manage_taxonomies_for_cpt_products_columns', 'funcy_test', 10, 3);
I expected it to return empty columns all around but instead I didn't see a result. One possible solution is to remove the Description cateogry entirely and use the custom_columns
hook to add my own description column but that just seems too much of a hassle.
Is there a way to limit the words / characters for a term description in the Admin Panel?
Share Improve this question asked Jul 3, 2014 at 21:26 Howdy_McGee♦Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges3 Answers
Reset to default 4Ok, I revisited this and revised my answer..
But like I said before:
the hook you are (trying) to use won't/can't work for the purpose you are aiming for, simply because its only occurrence is in
class-wp-posts-list-table.php
, which nomen est omen is used to show the post list, the right place should beclass-wp-terms-list-table.php
.
1. What you can do is the following:
Making use of the
admin_head-$hook_suffix
andget_terms
hookadd_action( 'admin_head-edit-tags.php', 'wpse152619_edit_tags_trim_description' ); function wpse152619_edit_tags_trim_description() { add_filter( 'get_terms', 'wpse152619_trim_description_callback', 100, 2 ); } function wpse152619_trim_description_callback( $terms, $taxonomies ) { // print_r($taxonomies); if( 'category' == $taxonomies[ 0 ] ) { foreach( $terms as $key => $term ) { $terms[ $key ]->description = wp_trim_words( $term->description, 12, ' [...]' ); } } return $terms; }
This trims the description to a specified word count.
2. Another often found possibility is removing the default column and create a new one:
Making use of the
manage_{$this->screen->taxonomy}_custom_column
andmanage_{$this->screen->id}_columns
hookfunction wpse152619_create_new_description_column( $columns ) { if ( $_GET[ 'taxonomy' ] == 'category' ) { // uncomment next line to remove default description column //unset( $columns[ 'description' ] ); // create new description column $columns[ 'new_description' ] = 'New Description'; return $columns; } return $columns; } add_filter( 'manage_edit-category_columns', 'wpse152619_create_new_description_column' ); function wpse152619_new_description_column_content( $deprecated, $column_name, $term_id ) { if ( $column_name == 'new_description' ) { $new_description = wp_trim_words( term_description( $term_id, $_GET[ 'taxonomy' ] ), 12, ' [...]' ); echo $new_description; } } add_filter( 'manage_category_custom_column', 'wpse152619_new_description_column_content', 10, 3 );
3. Also possible would be:
- Extend the class
WP_List_Table
yourself, likeWP_Terms_List_Table
it does; - In your case the main goal would be redefining the
column_description
method; - Additionally, create a new template, similar to
edit-tags.php
, but using your newly create class; - Then you would have to make sure that your new template is loaded instead of the default one;
- Ok, but that's just a outline of would be possible too. Makes no sense in this simple case, but for advanced use cases this would be an approach to consider.
I'm sure the above should solve your problem. Note that I did the examples for category edit page, but how to do this for custom taxonomies should be apparent. Depending on what is needed a simple or elaborate approach can be chosen. In you case the first one should be just fine.
Last but not least the following function is handy to get the information you need:
add_action( 'admin_head', 'wpse152619_dbg_dev' );
function wpse152619_dbg_dev() {
global $pagenow;
print_r( $pagenow );
echo '<br>';
print_r( $_GET[ 'taxonomy' ] );
echo '<br>';
$current_screen = get_current_screen();
print_r( $current_screen->id );
}
After some look around I found that you can put anything inside wp_trim_words:
<?php echo wp_trim_words( get_the_content(), 40, '...' ); ?>
Replace get_the_content() with your function, in your case you can use this:
<?php echo wp_trim_words( get_the_archive_description(), 30, '...' ); ?>
For more documentation check wp_trim_words()
A little cleaner reading solution.
add_action( 'admin_head-edit-tags.php', 'admin_edit_tags' );
function admin_edit_tags() {
add_filter( 'get_terms', 'admin_trim_category_description', 10, 2 );
}
function admin_trim_category_description( $terms, $taxonomies ) {
if ( !is_admin()) // only allow in admin
return;
if( 'category' != $taxonomies[0] )
return $terms;
foreach( $terms as $key=>$term )
// change 150 to whatever character length you want the description to be
$terms[$key]->description = substr( $term->description, 0, 150 );
return $terms;
}
orig. from https://www.isitwp/truncate-description-within-categories-admin-panel/
本文标签: Limit Words in CategoryTerm DescriptionAdmin Panel
版权声明:本文标题:Limit Words in CategoryTerm Description - Admin Panel 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741572010a2386065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论