【问题标题】:Append current category name to product title on woocommerce category archives将当前类别名称附加到 woocommerce 类别档案中的产品标题
【发布时间】:2019-05-21 01:53:00
【问题描述】:

我想在 woocommerce 中的产品名称中显示当前类别。仅适用于可变产品的内容。

下面的代码部分工作,但需要一些改革:

1 - 仅适用于可变产品。

2 - 显示查看者所在的当前类别不是主要产品类别。

remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
function loop_title() { 
global $post;?>
<div class="col-xl-3 col-md-3 col-sm-3">
    <h2><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title(); ?> <?php 
    $terms = get_the_terms( $post->ID, 'product_cat' );
    if ( $terms && ! is_wp_error( $terms ) ) :
        if ( ! empty( $terms ) ) {
            echo $terms[0]->name;
        }?>
    <?php endif;?></a></h2>

</div>
<?php }
add_action('woocommerce_shop_loop_item_title', 'loop_title', 10);

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce custom-taxonomy


    【解决方案1】:

    更新:以下内容应如您所愿:

    add_action( 'woocommerce_shop_loop_item_title', 'custom_shop_loop_item_title', 5 );
    function custom_shop_loop_item_title() {
        global $product;
    
        if ( $product->is_type('variable') && is_product_category() ) {
            remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
            add_action('woocommerce_shop_loop_item_title', 'custom_product_loop_title', 10);
        }
    }
    
    function custom_product_loop_title() {
        $taxonomy       = 'product_cat';
        $queried_object = get_queried_object();
        ?>
        <div class="col-xl-3 col-md-3 col-sm-3">
            <h2><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title();
            // For product category archives pages
            if( is_a($queried_object, 'WP_Term') && $queried_object->taxonomy === $taxonomy ) {
                echo ' ' . $queried_object->name;
            }
            ?></a></h2>
        </div>
        <?php
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢您的回复!唯一的问题是,其他单个产品名称不知何故完全从类别页面中消失了。
    • 代码在第 8 行丢弃了一些错误“语法错误,意外'}',期待 T_ENDIF”。
    • @Qwertzui11 好的,抱歉,多次更新......这次它工作得很好。
    • 干得好!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多