Passing PHP value into function

I apologize in advance for the lengthy question :slight_smile:

I'm trying to figure out how to modify the category list widget from Marketpress to only show the sub-categories in the current category. I modified the function for the widget and came up with this:

/**
 * 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;
	$args['show_option_none'] = __('No Sub-Categories');
		foreach ( $terms as $term ) {
			if ($term->name == 'Permanent Control'){
  				$args['child_of'] = 28;
				$args['title_li'] = '<h2>Permanent Control</h2>';
				break;
				}
			elseif ($term->name == 'Collecting Revenue'){
  				$args['child_of'] = 23;
				$args['title_li'] = '<h2>Collecting Revenue</h2>';
				break;
				}
			elseif ($term->name == 'First Class Service'){
  				$args['child_of'] = 26;
				$args['title_li'] = '<h2>First Class Service</h2>';
				break;
				}
			elseif ($term->name == 'Gorilla Post'){
  				$args['child_of'] = 29;
				$args['title_li'] = '<h2>Gorilla Post</h2>';
				break;
				}
			elseif ($term->name == 'GreenDrop Recycling'){
  				$args['child_of'] = 30;
				$args['title_li'] = '<h2>GreenDrop Recycling</h2>';
				break;
				}
			elseif ($term->name == 'Reducing Expenses'){
  				$args['child_of'] = 25;
				$args['title_li'] = '<h2>Reducing Expenses</h2>';
				break;
				}
			elseif ($term->name == 'Protecting Revenue'){
  				$args['child_of'] = 24;
				$args['title_li'] = '<h2>Protecting Revenue</h2>';
				break;
				}
			elseif ($term->name == 'Temporary Control'){
  				$args['child_of'] = 27;
				$args['title_li'] = '<h2>Temporary Control</h2>';
				break;
				}
		}
  $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';

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

and that seemed to work but on some items it uses the wrong category. Since I'm using it on category pages also, which show multiple posts, it seems to pull the array for each post. Since some items in my store are in multiple categories it sometimes pulls the wrong menu.

I would like to use the WP function single_cat_title(); to pass the value into the IF statements, since it's spot on every time. So then I came up with this:

/**
 * Creates a menu based on the current category
 *
 */
 function mp_category_menu( $echo = true, $args = '' ) {
	 global $post;
	$cat_name = single_cat_title('', false);

	$args['taxonomy'] = 'product_category';
  	$args['echo'] = false;
	$args['style'] = 'list';
	$args['hierarchical'] = true;
	$args['hide_empty'] = 0;
	$args['show_option_none'] = __('No Sub-Categories');
			if ($cat_name == 'Permanent Control'){
  				$args['child_of'] = 28;
				$args['title_li'] = '<h2>Permanent Control</h2>';
				break;
				}
			elseif ($cat_name == 'Collecting Revenue'){
  				$args['child_of'] = 23;
				$args['title_li'] = '<h2>Collecting Revenue</h2>';
				break;
				}
			elseif ($cat_name == 'First Class Service'){
  				$args['child_of'] = 26;
				$args['title_li'] = '<h2>First Class Service</h2>';
				break;
				}
			elseif ($cat_name == 'Gorilla Post'){
  				$args['child_of'] = 29;
				$args['title_li'] = '<h2>Gorilla Post</h2>';
				break;
				}
			elseif ($cat_name == 'GreenDrop Recycling'){
  				$args['child_of'] = 30;
				$args['title_li'] = '<h2>GreenDrop Recycling</h2>';
				break;
				}
			elseif ($cat_name == 'Reducing Expenses'){
  				$args['child_of'] = 25;
				$args['title_li'] = '<h2>Reducing Expenses</h2>';
				break;
				}
			elseif ($cat_name == 'Protecting Revenue'){
  				$args['child_of'] = 24;
				$args['title_li'] = '<h2>Protecting Revenue</h2>';
				break;
				}
			elseif ($cat_name == 'Temporary Control'){
  				$args['child_of'] = 27;
				$args['title_li'] = '<h2>Temporary Control</h2>';
				break;
				}
  $list = '<ul id="mp_category_list">' . wp_list_categories( $args ) . '</ul>';

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

The problem is it breaks the whole page. Anyone have any ideas on how I should pass the value into the function correctly?

  • Parking

    Here's what I've got for the menu function. Clunky for sure but it works in the top level categories and on the product pages. I just need to figure a way to get it to work on the sub-category pages

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

    Ok so I got it up and working. Its a little chaunce since it relies on another plugin to work, but for my purposes its fine for now.

    Here is the function for anyone interested:

    /**
     * Creates a menu based on the current category
     *
     */
     function mp_category_menu( $echo = true, $args = '' ) {
    	 global $post;
    	$bcnish  = ( bcn_display(true, false, false) );
    	$bcnsplit = explode(" > ", $bcnish);
    
    	$args['taxonomy'] = 'product_category';
      	$args['echo'] = false;
    	$args['style'] = 'list';
    	$args['hierarchical'] = true;
    	$args['hide_empty'] = 0;
    	$args['show_option_none'] = __('No Sub-Categories');
    			if ($bcnsplit[2] == 'Permanent Control'){
      				$args['child_of'] = 28;
    				$args['title_li'] = '<h2>Permanent Control</h2>';
    				}
    			elseif ($bcnsplit[2] == 'Collecting Revenue'){
      				$args['child_of'] = 23;
    				$args['title_li'] = '<h2>Collecting Revenue</h2>';
    				}
    			elseif ($bcnsplit[2] == 'First Class Service'){
      				$args['child_of'] = 26;
    				$args['title_li'] = '<h2>First Class Service</h2>';
    				}
    			elseif ($bcnsplit[2] == 'Gorilla Post'){
      				$args['child_of'] = 29;
    				$args['title_li'] = '<h2>Gorilla Post</h2>';
    				}
    			elseif ($bcnsplit[2] == 'GreenDrop Recycling'){
      				$args['child_of'] = 30;
    				$args['title_li'] = '<h2>GreenDrop Recycling</h2>';
    				}
    			elseif ($bcnsplit[2] == 'Reducing Expenses'){
      				$args['child_of'] = 25;
    				$args['title_li'] = '<h2>Reducing Expenses</h2>';
    				}
    			elseif ($bcnsplit[2] == 'Protecting Revenue'){
      				$args['child_of'] = 24;
    				$args['title_li'] = '<h2>Protecting Revenue</h2>';
    				}
    			elseif ($bcnsplit[2] == '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.