//Register new product taxonomy - Authors
function register_author_taxonomy() {
$labels = array(
\'name\' => __( \'Author\', \'taxonomy general name\' ),
\'singular_name\' => __( \'Author\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Authors\' ),
\'all_items\' => __( \'All Authors\' ),
\'parent_item\' => __( \'Parent Author\' ),
\'parent_item_colon\' => __( \'Parent Author:\' ),
\'edit_item\' => __( \'Edit Author\' ),
\'update_item\' => __( \'Update Author\' ),
\'add_new_item\' => __( \'Add New Author\' ),
\'new_item_name\' => __( \'New Author Name\' ),
\'menu_name\' => __( \'Authors\' ),
);
$args = array(
\'public\' => true,
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'authors\',
\'with_front\' => true,
),
\'show_admin_column\' => true
);
register_taxonomy("product_author", array("product"), $args );
}
add_action( \'init\', \'register_author_taxonomy\' );
// Register new product taxonomy - Publisher
function register_publisher_taxonomy() {
$labels = array(
\'name\' => __( \'Publisher\', \'taxonomy general name\' ),
\'singular_name\' => __( \'Publisher\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Publishers\' ),
\'all_items\' => __( \'All Publishers\' ),
\'parent_item\' => __( \'Parent Publisher\' ),
\'parent_item_colon\' => __( \'Parent Publisher:\' ),
\'edit_item\' => __( \'Edit Publisher\' ),
\'update_item\' => __( \'Update Publisher\' ),
\'add_new_item\' => __( \'Add New Publisher\' ),
\'new_item_name\' => __( \'New Publisher Name\' ),
\'menu_name\' => __( \'Publishers\' ),
);
$args = array(
\'public\' => true,
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'publishers\',
\'with_front\' => true,
),
\'show_admin_column\' => true
);
register_taxonomy("product_publisher", array("product"), $args );
}
add_action( \'init\', \'register_publisher_taxonomy\' );
// Add Custom Taxonomies to Product Pages
add_action(\'woocommerce_single_product_summary\',
\'custom_taxonomy_description\', 10 );
function custom_taxonomy_description() {
if (is_product) {
$authors = get_the_terms( $post->ID , \'product_author\' );
foreach ( $authors as $author ) {
$author_link = get_term_link( $author, \'product_author\' );
$publishers = get_the_terms( $post->ID , \'product_publisher\' );
foreach ( $publishers as $publisher ) {
$publisher_link = get_term_link( $publisher, \'product_publisher\' );
if( is_wp_error( $author_link, $publisher_link ) )
continue;
echo \'<br>\'. \'<div class="before-content">\'. \'Author: \'.\'<a href="\' . $author_link . \'">\' . $author->name . \'</a>\'. \'<br>\'. \'Publisher: \'.\'<a href="\' . $publisher_link . \'">\' . $publisher->name . \'</a>\'.\'</div>\'. \'<br>\';
}
我正在尝试向我的woocommerce产品中添加一些自定义分类法,本质上是一个书店,因此我需要作者、出版商等。我希望这些分类法显示在产品页面上,上面的代码正在执行所有这些操作,但是,当任一自定义分类法为空时,我会显示一条错误消息。
只是想知道是否有人有某种if(!empty())解决方案,在分类法为空时不会显示任何内容。
我正在使用的代码与许多其他不同的帖子非常纠结,因此任何简化的想法都将受到赞赏!
此外,不确定这是否相关,但我正在Wordpress上使用Genesis框架。
提前感谢!