简体   繁体   English

修改标准WordPress 4.4.2类别小部件以显示自定义分类法

[英]Modifying standard WordPress 4.4.2 Categories widget to display custom taxonomies

I have edited (a copy of course) /wp-includes/widgets/class-wp-widget-categories.php to force it to show the content of my custom taxonomy by just adding a single line (#59 as for the original file from WP 4.2.2) at the end of $cat_args = array();, so now it reads 我已经编辑(当然是一份副本)/wp-includes/widgets/class-wp-widget-categories.php,通过仅添加一行(#59与原始文件相同)来强制其显示我的自定义分类法的内容从WP 4.2.2开始)$ cat_args = array();的末尾,因此现在读取

$cat_args = array(
        'orderby'      => 'name',
        'show_count'   => $c,
        'hierarchical' => $h,
        'taxonomy'     => 'my_taxonomy'
    );

Nothing really difficult at this point, everything works as intended while the new widget set as list. 在这一点上没有什么真正困难的,当新的小部件设置为列表时,一切都按预期工作。

Everything ruins immediately after setting the new widget to display as dropdown menu as all items of the the dropdown list point to unexisting URLs. 设置新窗口小部件以显示为下拉菜单后,一切立即崩溃,因为下拉列表中的所有项均指向不存在的URL。

As my experience level (it is less the zero in fact. but I'm learning!) allows me to see, the problem hides somewhere in line #62 of the original widget 正如我的经验水平(实际上不是零),但我正在学习!),我发现问题隐藏在原始小部件的第62行中

$dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";

as this exact string in my opinion responsible for setting the values for dropdown list items according to the JS that starts at line #82. 因为我认为这个确切的字符串负责根据从第82行开始的JS设置下拉列表项的值。

I'm able to replace 我可以更换

?cat=

with

my_taxonomy/

at line #88 myself, but I need to put the correct value into $dropdown_id variable. 我本人在第88行,但是我需要将正确的值放入$ dropdown_id变量中。

And this is the problem for me. 这就是我的问题。

Can anybody help? 有人可以帮忙吗?

Question is very unclear. 问题很不清楚。 But if you are accessing the custom taxonomies that is categories using this method then it will be not shown. 但是,如果您使用此方法访问属于类别的自定义分类法,则不会显示该分类法。 Procedure of displaying custom taxonomies is a lot much different. 显示自定义分类法的过程有很多不同。

As I have discovered the solution myself I'll be the one who will answer my own question, just in case anyone could face the same problem. 当我自己发现解决方案时,我将成为回答我自己的问题的人,以防万一有人可能遇到相同的问题。

It is impossible to force the wp_dropdown_categories() to work with custom taxonomies without making a little modification to the Walker_CategoryDropdown class, due to its ancient bug or feature. 由于Walker_CategoryDropdown类的Walker_CategoryDropdown错误或功能,如果Walker_CategoryDropdown类进行少许修改,就不可能强迫wp_dropdown_categories()使用自定义分类法。

So I need to modify the Walker_CategoryDropdown class found in /wp-includes/class-walker-page-dropdown.php . 所以,我需要修改中发现的Walker_CategoryDropdown类/wp-includes/class-walker-page-dropdown.php

To preserve the original class-walker-page-dropdown.php I should copy its content to my plugin file. 为了保留原始的class-walker-page-dropdown.php,我应该将其内容复制到我的插件文件中。 Then I should replace 那我应该更换

$args['value_field'] = 'ID';

found on row 48 with 在第48行找到

$args['value_field'] = 'slug';

to make the modified class able to get the taxonomy slug instead of category ID and put it into the <option> 's value=" " term of my new widget. 使修改后的类能够获取分类标准标签,而不是类别ID,并将其放入新小部件的<option>的value =“”术语中。

Then I have deleted rows 1, 47 and 49 and replaced 然后我删除了第1、47和49行并替换了

class Walker_PageDropdown extends Walker {

with

class custom_walker_dropdown extends Walker_CategoryDropdown {

to define this modification as a new separate class. 将此修改定义为新的单独类。

And finaly I've set the new class as a Walker for wp_dropdown_categories() in my new widget based on WP's native Categories widget. 最后,我已经基于WP的本机类别小部件在新的小部件中将新类设置为wp_dropdown_categories()的Walker。

I have avoided setting the new class widget-wide as it would ruin the List part of the new widget, so the custom_walker_dropdown class should be set as walker only for wp_dropdown_categories() . 我避免在整个窗口小部件的范围内设置新类,因为这会破坏新窗口小部件的List部分,因此custom_walker_dropdown类应仅对wp_dropdown_categories()设置为walker。

The final step is to replace the row 87 of my modified copy of /wp-includes/widgets/class-wp-widget-categories.php 最后一步是替换/wp-includes/widgets/class-wp-widget-categories.php修改后的副本的第87行

if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {

with

if ( dropdown.options[ dropdown.selectedIndex ].value !== 0 ) {

to make the JS responsible for this dropdown menu working. 使负责此下拉菜单的JS起作用。

Bingo! 答对了! Everything works. 一切正常。

Actually there are tons of custom Walkers found by googling here and there but as for me the best way is to take the original class and make a little modification. 实际上,通过在各地搜索,可以找到大量定制的Walkers,但对我而言,最好的方法是参加原始课程并进行一些修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM