Category List Modification?

function mp_list_categories( $echo = true, $args = '' ) {
  $args['taxonomy'] = 'product_category';
  $args['echo'] = false;

  $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';

  if ($echo)
    echo $list;
  else
    return $list;
}

That is the function that creates the list of products when you use the widget.

I'm trying to figure out how to modify it to only show the products in a specific category. The end all would be to have it make the list based on ITS current category. I was trying to get the taxonomy and pass it into the menu function that way when your on a category page or product page, it gives you a list of the sub-categories. Here is my feeble attempt; keep in mind I'm new to programming in general and REALLY new to PHP, so be kind :slight_smile:

/**
 * Gets the taxonomy value
 *
 */
 function match_tax ( $echo = true ) {
	$terms = get_the_terms($post->ID, "product_category");
	return $terms;
}

/**
 * Creates a menu based on the current category
 *
 */
 function mp_category_menu( $echo = true, $args = '' ) {
  $args['taxonomy'] = 'gorilla-post';
  $args['echo'] = false;

  $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';

  if ($echo)
    echo $list;
  else
    return $list;
}

If someone can just edit the current mp_list_categories(); to show only the sub-categories within the category I specify, that would work. I can just write nested if's since I only have 8 categories.

Please help!

  • Parking

    Update #2

    This is working great to build the menu when the parent ID is hard coded.

    /**
     * Creates a menu based on the current category
     *
     */
     function mp_category_menu( $echo = true, $args = '' ) {
      $args['taxonomy'] = 'product_category';
      $args['echo'] = false;
      $args['child_of'] = getCurrentCatID();
      $args['title_li'] = '';
    
      $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';
    
      if ($echo)
        echo $list;
      else
        return $list;
    }

    I just need to figure out how to write a function to get the category ID whether your on a product page(post) or category page. Even if one function can't do both I can just use a conditional. Any help would be appreciated I love to get this thing done this weekend.

  • aecnu

    Greetings Parking,

    There is very limited support on the weekends as indicated below except for those of us that choose to come in and help outside the designated hours.

    Support staff hours are 9AM - 5PM CST (US) Monday - Friday... threads posted outside this time may experience response delays of 12 - 16 hours or longer at weekends.

    However, lets see what we can do.

    I could easily copy and paste the information in this ticket, but it would better serve you to give you the complete and direct reference for the category functions:

    http://codex.wordpress.org/Function_Reference/get_the_category

    Please advise how it goes with this project and if the reference material indicated above get us the solution to solve your coding issue.

    Thank you for being a WPMU Dev Member!

    Cheers, Joe

  • Parking

    Aaron,

    Thanks for the insight into this issue. I have already informed the client that products can only belong to one category, which they are fine with. Since I don't need to to worry about the first category part of it, this is what I came up with:

    function mp_category_menu( $echo = true, $args = '' ) {
      $args['taxonomy'] = 'product_category';
      $args['echo'] = false;
      $args['child_of'] = get_the_terms($post->ID, "product_category");
      $args['title_li'] = '';
    
      $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';
    
      if ($echo)
        echo $list;
      else
        return $list;
    }

    but it just returns No Category.

    I tested just the get_the_terms part on its own and it returns Array instead of an actual value, which I think is why the function returns no category. Do I need to manipulate the output somehow to get the value? Should I just put the get_the_terms part in its own function for the sake of manipulating the output, then call that function into my menu function?

  • Parking

    Aaron,

    I have tried to get this thing working but have had no luck thus far. this is my function:

    /**
     * Creates a menu based on the current category
     *
     */
     function mp_category_menu( $echo = true, $args = '' ) {
      global $post;
      $terms = get_the_terms($post->ID, "product_category");
      $first_term = $terms[0]->term_id;
    
      $args['taxonomy'] = 'product_category';
      $args['echo'] = false;
      $args['child_of'] = $first_term;
      $args['title_li'] = '';
    
      $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';
    
      if ($echo)
        echo $list;
      else
        return $list;
    }

    and I'm calling it with this:
    <?php mp_category_menu(); ?>
    in a php text widget in a sidebar.

    Am I doing something wrong?

  • Parking

    I thought this was working fine, but I realized in a couple cases it loads the wrong menu and I can't figure out why. Check out the site here: PZ Dev Notice that it is showing the "Protecting Revenue" menu even thought your not in that category. The first menu on the left I am using to try and diagnose. Its is using `<?php
    global $post;
    $terms = get_the_terms($post->ID, "product_category");

    print_r($terms)

    ?>`

    Any help would be awesome this site needs to go up ASAP and this is one of the bugs I am held up with.

    Here is my function:

    /**
     * Creates a menu based on the current category
     *
     */
     function mp_category_menu( $echo = true, $args = '' ) {
    	 global $post;
    	$terms = get_the_terms($post->ID, "product_category");
    
    	$args['taxonomy'] = 'product_category';
      	$args['echo'] = false;
    	$args['style'] = 'list';
    	$args['hierarchical'] = true;
    	$args['hide_empty'] = 0;
    
    		foreach ( $terms as $term ) {
    			if ($term->name == 'Permanent Control'){
      				$args['child_of'] = 28;
    				$args['title_li'] = '<h2>Permanent Control</h2>';
    				}
    			elseif ($term->name == 'Collecting Revenue'){
      				$args['child_of'] = 23;
    				$args['title_li'] = '<h2>Collecting Revenue</h2>';
    				}
    			elseif ($term->name == 'First Class Service'){
      				$args['child_of'] = 26;
    				$args['title_li'] = '<h2>First Class Service</h2>';
    				}
    			elseif ($term->name == 'Gorilla Post'){
      				$args['child_of'] = 29;
    				$args['title_li'] = '<h2>Gorilla Post</h2>';
    				}
    			elseif ($term->name == 'GreenDrop Recycling'){
      				$args['child_of'] = 30;
    				$args['title_li'] = '<h2>GreenDrop Recycling</h2>';
    				}
    			elseif ($term->name == 'Reducing Expenses'){
      				$args['child_of'] = 25;
    				$args['title_li'] = '<h2>Reducing Expenses</h2>';
    				}
    			elseif ($term->name == 'Protecting Revenue'){
      				$args['child_of'] = 24;
    				$args['title_li'] = '<h2>Protecting Revenue</h2>';
    				}
    			elseif ($term->name == 'Temporary Control'){
      				$args['child_of'] = 27;
    				$args['title_li'] = '<h2>Temporary Control</h2>';
    				}
    		}
      $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';
    
      if ($echo)
        echo $list;
      else
        return $list;
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.