potski Posted August 30, 2016 Share Posted August 30, 2016 Greetings, So I am working on making a widget, I have the option to hide categories from it. The list with options of what to hide consist of both categories and subcategories. Now, the problem is, if I tick a main category then all subcategories will automatically be shown also. Meaning nothing will be hidden. If I only tick subcategories without its parent, well then everything will be shown on the page as long as its parent is ticked. Any ideas on how I can enable hiding subcategories while showing its parent? <p> <label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Only show cities below'); ?>:</label> <?php echo '<input type="checkbox" name="'.$this->get_field_name('only_these').'" value="1" '.( $instance['only_these'] == "1" ? ' checked="checked" ' : "" ).' /> '; ?> </p> <p> <label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Cities to show'); ?>:</label> <div style=" width:220px; height:180px; background-color:#ffffff; overflow:auto;border:1px solid #ccc"> <?php $terms = get_terms("ad_city","ad_city=0&hide_empty=0"); foreach ( $terms as $term ) { echo '<input type="checkbox" name="'.$this->get_field_name('term_'.$term->term_id).'" value="'.$term->term_id.'" '.( $instance['term_'.$term->term_id] == $term->term_id ? ' checked="checked" ' : "" ).' /> '; echo $term->name.'<br/>'; Any help would be much appreciated :-) Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/ Share on other sites More sharing options...
Psycho Posted August 30, 2016 Share Posted August 30, 2016 There's no way to help you with what you have posted. All you have provided is some code to create the form. There is nothing in that code that has logic to show/hide anything that I can see. I have no idea if you have PHP or JavaScript code to do the show/hide functionality. Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536864 Share on other sites More sharing options...
potski Posted August 30, 2016 Author Share Posted August 30, 2016 (edited) Hmm okay well that clears something up lol. How about this: if(count($terms) < count($terms_k)) $disp_btn = 1; else $disp_btn = 0; $count = count($terms); $i = 0; if ( $count > 0 ){ echo "<ul id='location-stuff'>"; foreach ( $terms as $term ) { if($i%$nr == 0) echo "<li>"; $total_ads = 0; $terms2 = ''; $terms2 = get_terms("city","parent=".$term->term_id."&hide_empty=0"); $mese2 = ''; if($terms2 && $only_parents == false) { foreach ( $terms2 as $term2 ) { I tried playing with this line: if($terms2 && $only_parents == false). Like this, then all categories and subcategories show up. I tried replacing only_parents it to only_these but it didn't have much of an impact. I guess it needs a second command? Edited August 30, 2016 by potski Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536866 Share on other sites More sharing options...
potski Posted August 30, 2016 Author Share Posted August 30, 2016 Attached the full PHP file. Should have done that earlier. Changing $terms = get_terms("ad_city","parent=0&hide_empty=0"); to $terms = get_terms("ad_city","ad_city=0&hide_empty=0"); will change the options from only parents to both categories and subcategories but still same problem -> subcategories can't be hidden if its parent is ticked. widget6.php Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536868 Share on other sites More sharing options...
potski Posted August 31, 2016 Author Share Posted August 31, 2016 Maybe this is what needs to be adjusted: Instead of selecting "Cities to show" then making it "Cities to hide" would do the trick? Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536892 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 Let's start with the basics: So I am working on making a widget, You then posted code which clearly is part of some ecosystem (Wordpress apparently?) which you didn't write nor understand. You'll get a lot farther providing some transparency. It would have been better if you started with something like this? "Hello. I have a wordpress site, and I found this widget code which is close to what I want. It's part of "xyz" package (linked to original). What I would like to do is make something similar but with these differences. Some screenshots or mockups would be helpful. Some fundamentals seem to be in order. Native user interface interactivity is accomplished via javascript, perhaps in concert with some ajax. Just because you found something that appears to be close to what you want doesn't mean that there are hooks or options built into it to suddenly expose options you wish the original interface has but you haven't found yet. Consider this some friendly advice -- start with brutal honesty, and a lot more information about what you are trying to achieve and why, rather than throwing out some code you didn't write, and obfuscating the source and ecosystem it belongs to. People here who most likely could help you do coding for a living. They aren't here to customize code for free. This forum exists to help others learn to program and help themselves by doing so. Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536900 Share on other sites More sharing options...
potski Posted August 31, 2016 Author Share Posted August 31, 2016 Duly noted. Thanks for speaking your mind about it. I will try to look up its javascript and modify it from there. And yes it is a widget from a Wordpress theme that is discontinued (no support for it anymore so trying to figure it out whilst learning). Quote Link to comment https://forums.phpfreaks.com/topic/302044-hide-subcategories-widget/#findComment-1536903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.