我改变了我的功能。php文件添加了一个名为“autor”的新分类法。代码如下:
add_action( \'init\', \'create_autor_nonhierarchical_taxonomy\', 0 ); function create_autor_nonhierarchical_taxonomy() {
// Labels part for the GUI
$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\' ),
);
// Now register the non-hierarchical taxonomy like tag
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( \'woocommerce_single_product_summary\', \'show_product_autor\', 24 );
function show_product_autor(){
the_terms( $post->ID, \'autor\', \'<b>AUTOR: </b>\', \', \', \'<br />\' );
}
一切都很完美,但我希望出现在作者名字中的链接将重定向到作者页面(团队插件),而不是商店页面。如何编辑代码以将分类法链接到团队页面?例如,在此页面中
https://www.editorialufv.es/catalogo/territory-inhabited/ 如果单击作者Elena Farini的名字,您将被重定向到他的书(在本例中只有一本),而不是他的团队页面:
https://www.editorialufv.es/team/elena-farini/
最合适的回答,由SO网友:David Sword 整理而成
我认为有两种方法可以做到这一点:
自动:通过将自动术语名称与团队帖子标题匹配,假设自动术语与自定义帖子类型相同team
发布标题时,您可以找到与代码的关系,如:
function show_product_autor(){
// get this woo-comm\'s product author
$authors = wp_get_post_terms( get_the_ID() , \'autor\' );
// we know it\'ll just be one author, so use first object of array
$author = array_pop($authors);
// knowing the authors name, lets find the TEAM page
$authorTeamPg = get_page_by_title( $author->name, \'OBJECT\', \'team\' );
// now we know the authors page
$authorTeamPgLink = get_permalink( $authorTeamPg->ID);
// output
echo "Author: <a href=\'{$authorTeamPgLink}\'>{$author->name}</a>";
}
如果没有找到匹配项,上面没有错误处理,但很容易进行编码。但是,在您发送的示例中,术语名称是
Elena Farini
自定义post type团队为
Farini Elena
. 所以这个代码会让你失败除非你
ensure the names match 在后端。另一种方法(因此您无需确保文本匹配)是使用ID并手动建立term与团队成员的关系,因此:
手动:通过将团队ID分配给AUTOR的术语metaautor
您需要使用_term_meta
并创建一个下拉字段,列出作者及其ID的值。将从autor
术语的ID为team
成员
Here\'s a good article 关于设置术语meta-feilds。
一旦设置,在show_product_autor()
你会这样做:
function show_product_autor(){
// get this woo-comm\'s product author
$authors = wp_get_post_terms( $post->ID, \'autor\' );
// we know it\'ll just be one author, so use first object of array
$author = array_pop($authors);
// get the team page ID from _term_meta for this term author
$authorTeamID = get_term_meta( $author->term_id, \'autor\', true );
// get the full TEAM page for this author
$authorTeamPg = get_post( $authorTeamID );
// now we know the authors page
$authorTeamPgLink = get_permalink( $authorTeamPg->ID);
// output
echo "Author: <a href=\'{$authorTeamPgLink}\'>{$authorTeamPg->post_title}</a>";
}