当为空时隐藏自定义WooCommerce分类

时间:2018-09-18 作者:shanedaly001

//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框架。

提前感谢!

2 个回复
SO网友:Hans

get_the_terms() 退货FALSE 如果分类法中没有术语,那么您可以对此进行检查。

$authors = get_the_terms( $post->ID , \'product_author\' );
if ( FALSE != $authors ) :
    // Do something
endif;
如果要避免显示空术语,即没有附加对象的术语,请使用get_terms() 相反,因为您可以传递一个参数来过滤它们:

$terms = get_terms( 
   array(
     \'object_ids\' => $post->ID,
     \'taxonomy\' => \'product_author\',
     \'hide_empty\' => TRUE,
) );

SO网友:shanedaly001

当数组为空时,我收到的错误消息是无效的foreach语句。

因此,对我来说,快速的解决方法就是在执行任何操作之前检查数组是否为空。

if (!empty ($authors)) {
感谢您的投入,非常感谢!

如果我能把它整理好,我会稍后发布完整的代码。

再次感谢!

结束

相关推荐