删除单个产品页面WooCommerce WordPress中的类别列表

时间:2016-01-17 23:06:34

标签: wordpress woocommerce

我在functions.php中创建了一个功能,其中列出了类别页面中当前类别的项目。这是PHP代码:

<?php
class trueTopPostsWidget extends WP_Widget {
    /*
     * создание виджета
     */
    function __construct() {
        parent::__construct(
            'true_top_widget', 
            'SideBarCategories', // заголовок виджета
            array( 'description' => 'Desc' ) // описание
        );
    }

    /*
     * фронтэнд виджета
     */
    public function widget( $args, $instance ) {

        echo $args['before_widget'];
        global $wp_query;
        $cat_obj = $wp_query->get_queried_object();         
        if($cat_obj)    {
            $category_name = $cat_obj->name;
            $category_ID  = $cat_obj->term_id;
        }   



        function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
        $params = array(
            'hierarchical' => 1,
            'show_option_none' => '',
            'hide_empty' => $temp,
            'parent' => $parent_cat_ID,
            'taxonomy' => 'product_cat'
        );
        $subcats = get_categories($params);
        $c = count($subcats);
        if ($c>0):?>
        <div style="margin-top:20%;">
         <h2>Категории товаров</h2>
        </div>
        <?php endif; echo '<ul class="wooc_sclist">';
        foreach ($subcats as $sc) {
            $link = get_term_link( $sc->slug, $sc->taxonomy );
            echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
        }
        echo '</ul>';
        }
        woocommerce_subcats_from_parentcat_by_ID($category_ID);
    echo $c;
    echo $args['after_widget'];
    }

    /*
     * бэкэнд виджета
     */
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Заголовок</label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php 
    }

    /*
     * сохранение настроек виджета
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }
}

/*
 * регистрация виджета
 */
function true_top_posts_widget_load() {
    register_widget( 'trueTopPostsWidget' );
}
add_action( 'widgets_init', 'true_top_posts_widget_load' );
?>

但我遇到了一个问题。我不想在单个产品页面中列出类别。现在我可以看到此页面中的所有类别。它对我的条件不满意。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:

<div class="unit-30">
    <div class="blok">
        <?php /* Widgetized sidebar */
            $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $look_for = "tovar";
            $pos = strpos($actual_link,$look_for);
            if ($pos==null) 
            if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Сайтбар woocommerce') ) : ?>
            <?php endif; ?>
    </div>
</div>