简体   繁体   English

WordPress的木材添加自定义分类到菜单

[英]Wordpress Timber add Custom Taxonomy to Menu

In a Timber based Wordpress project, I need to add programaticly all Terms(with link) of a custom taxonomy(victim groups) in a TimberMenu as children of the menu point victims(a custom post type). 在基于Timber的Wordpress项目中,我需要以编程方式在TimberMenu中添加自定义分类法(受害者组)的所有条款(带有链接)作为菜单点受害者(自定义帖子类型)的子级。

Is there an (elegant) way to do this? 有(优雅)的方法可以做到这一点吗?

Wordpress Version : 4.4.2 Timber Version: 0.22.5 WordPress版本:4.4.2木材版本:0.22.5

=======UPDATE======== =======更新========

For example: in my taxonomy term (victim groups) I have the terms a,b,c,d,e 例如:在我的分类学术语(受害者组)中,我有术语a,b,c,d,e

now I want to add a menu item called victim groups with the child items a,b,c,d and e 现在,我想添加一个名为“受害者组”的菜单项,以及子项a,b,c,d和e

So that I can klick on any of a,b,c,d or e to get a page with all posts related to this term. 这样我就可以在a,b,c,d或e中任意一个上一个页面,以获得与此术语相关的所有帖子。

The customer isn't allowed to set menu items, so that I have to set them programmaticly and auto add all new terms of this taxonomy(victim groups) to it. 不允许客户设置菜单项,因此我必须以编程方式设置它们,并自动向其添加此分类法(受害者组)的所有新术语。 cheers and thanks in advance bambamboole 欢呼,并预先感谢bambamboole

I normally do this kind of things through the wp_get_nav_menu_items filter. 我通常通过wp_get_nav_menu_items过滤器执行此类操作。 When I tried this first, it took me quite some time to get how I can mock up an existing WP_Post or WP_Term to be used as a nav menu item. 当我第一次尝试时,花了我相当多的时间来了解如何模拟现有的WP_Post或WP_Term用作导航菜单项。 You can't just add the item itself, but you need to adapt it a little so that the nav item still links to the right destination. 您不仅可以添加项目本身,还需要对其稍加调整,以使导航项目仍链接到正确的目的地。 Also, if you just add menu items it can mess up your menu order. 另外,如果您仅添加菜单项,则可能会使您的菜单顺序混乱。 So we have to rebuild the menu array with our own menu order counter. 因此,我们必须使用我们自己的菜单顺序计数器来重建菜单数组。

I have used the following combination of a helper function and variations of the wp_get_nav_menu_items filter in a couple of projects to quickly add new items to my menus, be it custom post types or taxonomies. 我在几个项目中结合使用了辅助功能和wp_get_nav_menu_items过滤器的变体的以下组合,以快速将新项目添加到菜单中,无论是自定义帖子类型还是分类法。 It worked great so far. 到目前为止效果很好。 This approach should work whether you use the standard WP Nav Menu Walker or TimberMenu to display your menus in your theme. 无论您使用标准的WP Nav菜单导航器还是TimberMenu来在主题中显示菜单,此方法都应有效。

The helper function 辅助功能

/**
 * Prepare a post or term object to be used as a WP nav menu item.
 *
 * @param string $type                  The type of the object added to the menu. Can be 'page',
 *                                      'category', 'taxonomy' or empty, assuming a custom post type
 * @param WP_Post|WP_Term $menu_post    The object you want to add that is converted
 *                                      to a menu item
 * @param int $menu_parent              The parent menu item you want to add the object to
 * @param int $menu_order_counter       The current menu order counter
 * @param bool $set_new_id              Whether to overwrite the current menu id. You normally want to
 *                                      do this, if you don’t want menu items to disappear randomly.
 * @return void
 */
function pre_setup_nav_menu_item( $type = '', &$menu_post, $menu_parent, $menu_order_counter, $set_new_id = true ) {
    $menu_post->menu_item_parent = $menu_parent;
    $menu_post->menu_order       = $menu_order_counter;
    $menu_post->post_type        = 'nav_menu_item';

    if ( 'page' == $type ) {
        $menu_post->object_id = $menu_post->ID;
        $menu_post->object    = 'page';
        $menu_post->type      = 'post_type';

    } else if ( 'category' == $type ) {
        $menu_post->object_id = $menu_post->term_id;
        $menu_post->object    = 'category';
        $menu_post->type      = 'taxonomy';

    } else if ( 'taxonomy' == $type ) {
        $menu_post->object_id = $menu_post->term_id;
        $menu_post->object    = $menu_post->taxonomy;
        $menu_post->type      = 'taxonomy';

    // Assuming a custom post type
    } else {
        // Use TimberPost if Timber exists
        if ( class_exists( 'Timber' ) ) {
            $menu_post = new TimberPost( $menu_post );
        }

        $menu_post->object_id = $menu_post->ID;
        $menu_post->object    = $type;
        $menu_post->type      = 'post_type';
    }

    /**
     * Create unique ID because in some cases the ID
     * will act as an array key. This way, no menu objects
     * should be overwritten.
     */
    if ( $set_new_id ) {
        $menu_post->ID = uniqid();
    }
}

The filter 过滤器

We take all the existing menu items and loop over them. 我们接受所有现有的菜单项并遍历它们。 When the menu item "Victims" is found, we get all the Victim Group terms and add them as submenu items. 找到菜单项“受害者”后,我们将获得所有受害者组条款并将其添加为子菜单项。 To be able to find the right menu item, you would create a page "Victims" which you then add manually to your Menu. 为了能够找到正确的菜单项,您将创建一个“受害者”页面,然后将其手动添加到菜单中。 In the following filter, you will have to set the page id of the page "Victim" as well as the name with which you registered your custom taxonomy. 在下面的过滤器中,您将必须设置页面“受害者”的页面ID以及用于注册自定义分类法的名称。

/**
 * Filter through nav menu items and add child items and anchor links accordingly
 */
add_filter( 'wp_get_nav_menu_items', function( $items, $menu, $args ) {
    /**
     * The page id of the page that was added manually to the menu
     * and should hold the custom taxonomy menu items.
     */
    $victim_page_id = 22;

    // Name of the custom taxonomy victim groups
    $victim_groups_tax_name = 'victimgroup';

    /**
     * Menus in Admin would also be affected if we wouldn’t
     * check if we’re on the frontend
     */
    if ( ! is_admin() ) {
        // Array to hold the newly built nav menu items array
        $new_items = array();

        // Integer to store custom menu order as we build up the new array
        $menu_order_counter = 1;

        /**
         * Loop through existing menu items and add them to custom menu
         */
        foreach ( $items as $item ) {
            // Add items in normal order
            $item->menu_order = $menu_order_counter;
            $new_items[] = $item;

            $menu_order_counter++;

            // Check for the menu item we want to add our submenu items to
            if ( (int) $item->object_id == $victim_page_id ) {
                // Victim Groups take the current menu item as a parent
                $menu_parent = $item->ID;

                // Get victim groups taxonomy terms
                $terms = Timber::get_terms( $victim_groups_tax_name, array(
                    // You probably want to hide empty taxonomies
                    'hide_empty' => true,
                ) );

                // Loop through terms found and add them as menu items
                foreach ( $terms as $term ) {
                    $term->post_title   = $term->name;
                    $term->post_parent  = $term->parent;

                    pre_setup_nav_menu_item( 'taxonomy', $term, $menu_parent, $menu_order_counter );
                    $term = new TimberTerm( $term );

                    $new_items[] = wp_setup_nav_menu_item( $term );

                    $menu_order_counter++;
                    unset( $term );
                }
            }
        }

        return $new_items;
    }

    return $items;
}, 10, 3);

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

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