我想在产品类别的后端表中添加一个新列。此列将包含“视图类别”链接,并将所有链接到www.domain.com/category/category-name页面。
我查看了Wordpress文档,这是我提出的代码......但它不起作用!
function product_cat_cpt_columns($columns) {
$new_columns = array(
'Link' => "Link to page"
);
return array_merge($columns, $new_columns);
}
add_filter('manage_product_cat_posts_custom_columns' , 'product_cat_cpt_columns');
知道怎么做吗?我真的很感谢你的帮助!
答案 0 :(得分:4)
从此answer拉出,您可以使用以下代码将列添加到“编辑标签”屏幕:
function add_post_tag_columns($columns){
$columns['foo'] = 'Foo';
return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_post_tag_columns');
function add_post_tag_column_content($content){
$content .= 'Bar';
return $content;
}
add_filter('manage_product_cat_custom_column', 'add_post_tag_column_content');
答案 1 :(得分:3)
我发现很难找到解决这个微不足道任务的办法,我非常感谢Helgatheviking的回答,它指出了正确的方向。她的回答对我来说不太有用,因为它只允许所有列值的相同值,所以我决定在这里发布一个改进的版本。
问题在于第二个函数,因为它没有提供添加与当前类别相对应的字段值的方法。我挖了Woocommerce's source(在那里你可以搜索“product_cat_column”来查看相关部分并查看它是如何制作的)并发现这个过滤器接受3个参数,而不是1.这允许每个特定值行,与所有行的值不同,就像在Helgatheviking的答案中一样。
另一个缺点是它会将值放到缩略图列上,因为这就是Woocommerce实际使用此过滤器的原因。
所以这是我的代码:
function add_custom_column($columns) {
$columns['foo'] = 'FOO';
$columns['link'] = 'Link to page';
return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_custom_column');
function category_custom_column_value( $columns, $column, $term_id ) {
if ($column == 'FOO') {
$foo = get_term_meta( $term_id, 'foo', true );
return $foo;
}elseif ($column == 'link') {
$category = get_term_by( 'id', $term_id, 'product_cat' );
$category_link = get_term_link( $category->slug, 'product_cat' );
return '<a href="' . $category_link . '" target="_blank">' . $category_link . '</a>';
}
}
add_filter('manage_product_cat_custom_column', 'category_custom_column_value', 10, 3);
如您所见,第一个函数保持不变,但第二个函数现在检查列名并根据此名称返回内容。您可以通过这种方式获取任何类别元素,并根据您的需要为多个列执行此操作。