• losrack

    (@carlos-jaramillo)


    Hi,

    I read this tutorial for building a plugin widget to show categories.

    The thing is that I would love this php file to show woocommerce categories instead

    Here is the code:

    <?php
    /**
     * Plugin Name:   Category 2 Columns
     * Plugin URI:    
     * Description:   Displays categories in 2 columns
     * Version:       1.0
     * Author:        
     * Author URI:    
     */
    
    /* widget function*/
    function widget( $args, $instance ) {
      // All widget output will go here
    }
    
    /*Variables*/
    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
      $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
        ) );
      $cat_count = 0;
      $cat_col_one = [];
      $cat_col_two = [];
      
      
    /* Function to output categories*/
    
    foreach( $categories as $category ) {
      $cat_count ++;
      $category_link = sprintf( 
          '<li class="list-unstyled"><a href="%1$s" alt="%2$s">%3$s</a></li>',
          esc_url( get_category_link( $category->term_id ) ),
          esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
          esc_html( $category->name )
      );
      if ($cat_count % 2 != 0 ) {
        $cat_col_one[] = $category_link;
      } else {
        $cat_col_two[] = $category_link;
      }
    }
    
    /* columns category print*/
    echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
    
    <div class="row">
    
      <div class="col-lg-6"><?php
        foreach( $cat_col_one as $cat_one ) {
          echo $cat_one;
        } ?>
      </div>
    
      <div class="col-lg-6"><?php 
        foreach( $cat_col_two as $cat_two ) {
          echo $cat_two;
        } ?>
      </div>
    
    </div><?php
    echo $args['after_widget'];
    ?>
    

    I would be really thankful if any one could help me out

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘php plugin help’ is closed to new replies.