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
    • Flash Drive

    just an update to this....

    wp_list_categories( $args )

    That is the code that creates the list, the taxonomy argument tells it what categories to list. I totally make this work with nested if's and inclusion arguments but the inclusion requires the ID number, which I don't know how to find.

    So Ill phrase another question....Anyone know how to find the category ID number for MP categories?

  • Parking
    • Flash Drive

    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
    • WP Unicorn

    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
    • Flash Drive

    I've been messing around with every function that remotely could work, including get_the_category but I can't get it to work. I'm not really sure why, I think I'm not applying it correctly within the function. I'm still very new to programming so sometimes the issues elude me. If you could give me an example of how to apply it, that would be great!

  • Parking
    • Flash Drive

    Looking further into this I noticed it says it only returns the default 'category' taxonomy. MP uses a custom taxonomy so I would have to use get_the_term() but as far as I can tell that requires hard coding of the post id or slug or something. Which is what I'm trying to make it return on it's own.

    I know support hours are limited any help is appreciated.

  • aecnu
    • WP Unicorn

    Greetings Parking,

    I truly want to help you with this but do not have an answer.

    However, I have messaged the lead developer of the Market Press plugin to see if I can get him in here to offer some of his invaluable advice regarding this issue.

    Though this may take longer then a normal ticket, I hope to get him in here asap.

    Sincerely, Joe

  • Aaron
    • CTO

    Which I see you already have up there. The trick part to all this is you can have multiple cats assigned to a post. So you could either just show a list for the first cat, or a different list for each cat.

    global $post;
    $terms = get_the_terms($post->ID, "product_category");
    $first_term = $terms[0]->term_id;
    mp_list_categories( true, array('child_of' => $first_term) );
  • Parking
    • Flash Drive

    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
    • Flash Drive

    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
    • Flash Drive

    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.