我在函数中添加了自定义分类法。php。代码如下:
function create_autor_nonhierarchical_taxonomy() {
$labels = array(
\'name\' => _x( \'Autor\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Autor\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Buscar autores\' ),
\'popular_items\' => __( \'Autores populares\' ),
\'all_items\' => __( \'Todos los autores\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Editar autor\' ),
\'update_item\' => __( \'Actualizar autor\' ),
\'add_new_item\' => __( \'Añadir nuevo autor\' ),
\'new_item_name\' => __( \'Nombre del nuevo autor\' ),
\'separate_items_with_commas\' => __( \'Separa los autores con comas\' ),
\'add_or_remove_items\' => __( \'Añadir o eliminar autores\' ),
\'choose_from_most_used\' => __( \'Elije ente los autores más utilizados\' ),
\'menu_name\' => __( \'Autor\' ),
);
register_taxonomy( \'autor\', \'product\', array(
\'hierarchical\' => false,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'update_count_callback\' => \'_update_post_term_count\',
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'autor\' ),
));
}
add_action( \'init\', \'create_autor_nonhierarchical_taxonomy\', 0 );
function show_product_autor(){
$authors = wp_get_post_terms( get_the_ID(), \'autor\' );
$author = array_pop($authors);
$authorTeamPg = get_page_by_title( $author->name, \'OBJECT\', \'team\' );
$authorTeamPgLink = get_permalink( $authorTeamPg->ID);
echo "<b>AUTOR: </b><a href=\'{$authorTeamPgLink}\'>{$author->name}</a>",\'<br />\';
}
add_action( \'woocommerce_single_product_summary\', \'show_product_autor\', 24 );
现在,我有超过一位作者的书。如何更改代码以显示实例2或3个作者?此外,有些书有一个编辑,而不是一个作者(一本书有许多作者,只有一个负责出版)。通常在这种情况下,编辑器的名称后面跟着一个括号,如(ed.)。如何将此选项添加到“作者”字段?
最合适的回答,由SO网友:inarilo 整理而成
要显示多个作者,请遍历作者数组:
function show_product_autor(){
$authors = wp_get_post_terms( get_the_ID(), \'autor\' );
foreach($authors as $author) {
$authorTeamPg = get_page_by_title( $author->name, \'OBJECT\', \'team\' );
$authorTeamPgLink = get_permalink( $authorTeamPg->ID);
echo "<b>AUTOR: </b><a href=\'{$authorTeamPgLink}\'>{$author->name}</a>",\'<br />\';
}
}
要处理编辑器,IMO最简单的解决方案是使分类法成为继承体系并添加一个术语
editor
所有同时也是编辑的作者。选择编辑器时,请同时选择作者名称及其下的“编辑器”术语。
因此,上述代码变为:
function show_product_autor(){
$authors = wp_get_post_terms( get_the_ID(), \'autor\');
$output = array();
foreach($authors as $author) {
if(!$author->parent) { //if there is no parent term
$authorTeamPg = get_page_by_title( $author->name, \'OBJECT\', \'team\' );
$authorTeamPgLink = get_permalink( $authorTeamPg->ID);
$output[$author->term_id][\'url\'] = "<a href=\'{$authorTeamPgLink}\'>{$author->name}</a>";
} else {
$output[$author->parent][\'ed\'] = \' (ed.)\';
}
}
$outputfinal = array();
foreach($output as $line) {
if(!empty($line[\'url\'])) { //just to be safe, check the url is there
$outputfinal[] = (empty($line[\'ed\'])) ? $line[\'url\'] : $line[\'url\'].$line[\'ed\'];
}
}
echo \'<b>AUTOR: </b>\'.implode(\', \', $outputfinal).\'<br />\';
}