Guys and girls, what month have I been trying to revive my wordpress site. Clearly I see that I still the same bydlokoder. Help me to implement such a thing that I did, just more normal code (: D):

So there is: There is a "system" of regions on the site, the selected region is a category. That is, the user is invited to select 6 categories, and a redirect is made to the selected one. There is a menu consisting of 4 categories: Rent, Sale, Exchange, Purchase. When you click on one of the categories, subcategories appear, but those that belong to the category that you clicked on in the menu, as well as those belonging to the already selected region. That is, in general, to present a picture of nesting categories:

Moscow

-Rent

- For a long time

-Exchange

--Apartments

Peter

-Rent

- For a short time

-Purchase

--Rooms

I hope it is clear. How did I:

<div id="menu1"> //Π±Π»ΠΎΠΊ мСню <div id="order-cat" class="order"> //Π±Π»ΠΎΠΊ ΠΏΠ΅Ρ€Π²ΠΎΠΉ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ Π°Ρ€Π΅Π½Π΄Ρ‹ */ if(is_category() and get_query_var('cat') == '10') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 30,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '11') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 31,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '34') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 37,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '33') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 38,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '15') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 32,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '35') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 36,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } ?> </div> <div id="order-cat" class="sell"> //Π±Π»ΠΎΠΊ Π²Ρ‚ΠΎΡ€ΠΎΠΉ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΏΡ€ΠΎΠ΄Π°ΠΆΠΈ */ if(is_category() and get_query_var('cat') == '10') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 40,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '11') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 43,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '34') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 41,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '33') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 42,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '15') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 44,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '35') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 39,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } ?> </div> <div id="order-cat" class="buyme"> //Π±Π»ΠΎΠΊ 3ΠΉ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΏΠΎΠΊΡƒΠΏΠΊΠ° */ if(is_category() and get_query_var('cat') == '10') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 46,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '11') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 49,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '34') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 47,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '33') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 48,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '15') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 50,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '35') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 45,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } ?> </div> <div id="order-cat" class="changeme"> //Π±Π»ΠΎΠΊ 4 ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΎΠ±ΠΌΠ΅Π½Π° */ if(is_category() and get_query_var('cat') == '10') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 52,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '11') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 55,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '34') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 53,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '33') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 54,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '15') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 56,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } else if(is_category() and get_query_var('cat') == '35') { $args = array('style'=>'none','show_count'=> 1,'hide_empty'=> 0,'use_desc_for_title' => 0,'child_of'=> 51,'hierarchical'=> true,'title_li'=>'','number'=>7); wp_list_categories($args); } ?> </div> </div> 

get_query_var ('cat') == '10', get_query_var ('cat') == '11', etc. are identifiers of the category-region. And in each variable $ args: 'child_of' => 45, 'child_of' => 44, etc. - this is the ID of the child headings, namely rent, sale, purchase, exchange, which in turn are subcategories from categories- regions.

Oh, I hope that at least someone understood what I mean :) In the end, someone will help to somehow reduce the code, and stop being a coder :)

2 answers 2

Simplifying duplicate data sets is simple:

 $categoryList = array( 10 => 30, 11 => 31 34 => 37 33 => 38 15 => 32 35 => 36); 

And so we fill as necessary

  $cat = get_query_var('cat'); if(is_category() and in_array( $cat , $categoryList)) { $args = array( 'style'=>'none', 'show_count'=> 1, 'hide_empty'=> 0, 'use_desc_for_title' => 0, 'child_of'=> $categoryList[$cat], 'hierarchical'=> true, 'title_li'=>'', 'number'=>7); wp_list_categories($args); } 

I think this way the volume of the text will decrease at times. With the same functionality. I am not strong in WordPress, but I think there is an opportunity to somehow separate the template from the logic, because it is so confusing. But in general, you will not have to write this bunch of if-s. Which is now present

  • Thank you very much. Exactly what is needed. - Eugene Shilin
  • I do not know, for some reason it does not work ... There are no mistakes either. Just does not display. - Eugene Shilin
  • You would show that you ended up because it is not clear how you used it. - binliz
  • answered below. - Eugene Shilin
 <div id="menu1"> <div id="order-cat" class="order"> <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ Π°Ρ€Π΅Π½Π΄Ρ‹ */ $cat = get_query_var('cat'); $categoryList = array(10 => 30, 11 => 31, 34 => 37, 33 => 38, 15 => 32, 35 => 36); if(is_category() and in_array($cat, $categoryList)) { $args = array('style'=>'none', 'show_count'=> 1, 'hide_empty'=> 0, 'use_desc_for_title' => 0, 'child_of' => $categoryList[$cat], 'hierarchical'=> true, 'title_li'=>'', 'number'=>7 ); wp_list_categories($args); } ?> </div> <div id="order-cat" class="sell"> <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΏΡ€ΠΎΠ΄Π°ΠΆΠΈ */ $cat = get_query_var('cat'); $categoryList = array(10 => 40, 11 => 43, 34 => 41, 33 => 42, 15 => 44, 35 => 39); if(is_category() and in_array($cat, $categoryList)) { $args = array('style'=>'none', 'show_count'=> 1, 'hide_empty'=> 0, 'use_desc_for_title' => 0, 'child_of' => $categoryList[$cat], 'hierarchical'=> true, 'title_li'=>'', 'number'=>7 ); wp_list_categories($args); } ?> </div> <div id="order-cat" class="buyme"> <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΏΡ€ΠΎΠ΄Π°ΠΆΠΈ */ $cat = get_query_var('cat'); $categoryList = array(10 => 46, 11 => 49, 34 => 47, 33 => 48, 15 => 50, 35 => 45); if(is_category() and in_array($cat, $categoryList)) { $args = array('style'=>'none', 'show_count'=> 1, 'hide_empty'=> 0, 'use_desc_for_title' => 0, 'child_of' => $categoryList[$cat], 'hierarchical'=> true, 'title_li'=>'', 'number'=>7 ); wp_list_categories($args); } ?> </div> <div id="order-cat" class="changeme"> <?php /* Если ΠΌΡ‹ Π² ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ ΠΈ катСгория - Ρ€Π΅Π³ΠΈΠΎΠ½, Ρ‚ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ ΠΏΡ€ΠΎΠ΄Π°ΠΆΠΈ */ $cat = get_query_var('cat'); $categoryList = array(10 => 52, 11 => 55, 34 => 53, 33 => 54, 15 => 56, 35 => 51); if(is_category() and in_array($cat, $categoryList)) { $args = array('style'=>'none', 'show_count'=> 1, 'hide_empty'=> 0, 'use_desc_for_title' => 0, 'child_of' => $categoryList[$cat], 'hierarchical'=> true, 'title_li'=>'', 'number'=>7 ); wp_list_categories($args); } ?> </div> </div> 

There is no error, it just does not print anything. If you add an else to the if and print the text, it displays it. Therefore, if does not pass. So in_array ($ cat, $ categoryList) does not work. get_query_var ('cat') returns a string, as it were, but framing quotes in the $ categoryList array does not help. Tell me what to do?

  • one
    Judging from your code, you are again engaged in bydo-coder. $ cat = get_query_var ('cat'); Enough to get once at the very top of the page. You say that $ cat returns: $ cat = intval (get_query_var ('cat')); or so $ cat = (int) get_query_var ('cat'); In any case, it will be right. Further, since I wrote the code without checking now I just noticed. We are looking for in the values ​​of the array that is not. We need to look in the keys: in_array ($ cat, array_keys ($ categoryList)), in principle, we’ll translate badly again: array_key_exists ($ cat, $ categoryList); And you should succeed. - binliz
  • Thank you very much, now I will try, accomplish your goal. - Eugene Shilin
  • Everything works fine, thanks! - Eugene Shilin
  • one
    I would have simplified it. It’s worth checking once and not 4. And form an array like this: $ categoryList = array (10 => array ('order' => 30, 'sell' => 40 ...), ...); - binliz
  • I don't know how to thank you, thanks again) - Eugene Shilin