Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When you add a category in the admin-panel, the adminstrator can select it's parent. Something like:

- Category1
-- Subcategory1 to Category1
-- Subcategory2 to Category1
-- Subcategory3 to Category1
....
- Category2
-- Subcategory1 to Category2
-- Subcategory2 to Category2
-- Subcategory3 to Category2
.... 

I have created a custom taxonomy. Is there a way to change these options to be only parent - selection like this for this specific taxonomy?

- Category1
- Category2
.... 

?

(Is there a hook for this for a taxonomy at all?)

share|improve this question
1  
It can be done but you're going to have to get a bit tricky. You should be able to get the gist of it from the non-hierarchical version of your answer (shibashake.com/wordpress-theme/…). You more or less need to apply the same concept to categories and add the JS handler to populate the second select box. –  Imperative Ideas Oct 10 '13 at 8:32
    
@ImperativeIdeas - Thanks for your línk! I thought the basic idea behind that link was very good, but I think it complicated to much for that special case I had. I've answered myself, so someone else hopefully might got helped in the future. –  bestprogrammerintheworld Oct 10 '13 at 15:27
add comment

1 Answer

If someone else stumble upon this kind of question, here's my solution to it: (without having to mangage any metaboxes etc)

add_action('categorycourses_add_form_fields','categorycourses_edit_form_fields');
add_action('categorycourses_edit_form_fields','categorycourses_edit_form_fields');

//Function only showing parent categories when editing or adding new category-courses
function categorycourses_edit_form_fields() {
    ?>
    <script type="text/javascript">
    jQuery(document).ready( function($) {
       var level = 1;
       //Go down in hierarchy from level1 and remove each level until there are no levels anymore
       //Don't do anything with level-0 because we want to show parent-categories
       while ( level < $("form .form-field #parent").find(".level-" +  level).length>0 ) { 
            $("form .form-field #parent").find(".level-1").remove(); //Remove level
       }
    });
    </script>    
<?php
}

(categorycourses is a taxonomy, and if you wish to use the code just change the taxonomy like

add_action('{taxonomy}_add_form_fields','{taxonomy}_edit_form_fields');
add_action('{taxonomy}_edit_form_fields','{taxonomy}_edit_form_fields');

)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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