I have a plugin install called uCan post so registered users can post and what not, but I am wanting to have two category lists for them to choose from.

one is a country

the other is an activity list.

Now I have the two lists available but I want to exclude certain category ID's from one of the lists.

Here is the code for the category lists.

  <br />

        <?php echo __('Select Your Experience', 'ucan-post'); ?>:
      <select name="ucan_submission_category">
          foreach($categories as $category)
            if($category->cat_ID == $_POST['ucn_submission_category'])
              echo '<option value="'.$category->cat_ID.'" selected="selected">'.$category->name.'&nbsp;&nbsp;</option>';
              echo '<option value="'.$category->cat_ID.'"">'.$category->name.'&nbsp;&nbsp;</option>';

Any help would be greatly appreciated!

Thank you

  • Where is $categories defined, and how is it defined? – Mark Duncan Jul 28 '11 at 17:41
  • Sorry I dont know too much about php, how would I find this out, I'm just looking in the plugin code right now. – Art Jul 28 '11 at 17:53
  • Same file you pulled the above code from, just further up perhaps? You're looking for a line that starts with $categories = .. – Mark Duncan Jul 28 '11 at 19:17
  • There wasnt in that file but in another file it says $categories = $this->uCan_Get_Categories(); – Art Jul 28 '11 at 19:22
  • Check the uCan plugin settings page(in your admin area), there's an exclude option for categories. – Mark Duncan Jul 28 '11 at 19:24

Ok then time to move my response into an answer, because it seems you just want to exclude categories from the loop in the above code..

Assuming you're alright to modify this file(a plugin update would overwrite it), then simply conditionalise your loop like so..

$excluded = array( 1, 2, 3 );
foreach($categories as $category)
    if( in_array( $category->cat_ID, $excluded ) )
        echo '<option value="'.$category->cat_ID.'"'. selected( $category->cat_ID, $_POST['ucn_submission_category'], false ).'>'.$category->name.'&nbsp;&nbsp;</option>';

Untested, so let me know if any problems.

Replace 1,2 and 3 with the IDs of the categories you wish to exclude.

| improve this answer | |
  • Ok I have done what you said and this has come up as a problem, here is the example = wordpress.art-williams.com/add-destination also, can you see what I am trying to do, by having the two category select bits. I appreciate all the help youve done so far – Art Jul 28 '11 at 20:22
  • Ok I have now seen where I have placed it properly, and no it doesnt work. It doesnt exclude the category ID's – Art Jul 28 '11 at 20:59
  • It should do... i've written loops like that more times than i can remember. Help me help you, what does "doesn't work" mean, can you expand on that? – t31os Jul 29 '11 at 8:22
  • I placed it were I should and it doesnt exclude the categories, I have changed the cat ID's and they still show – Art Jul 29 '11 at 8:53
  • Yes, sorry i made a slight mistake in the code, try it now. – t31os Jul 31 '11 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.