1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Dynamically Created Vendor Menus

Dynamically Created Vendor Menus

This code will add a list of all vendors, as a menu item, on your site. Have fun, and tweak it any way you see fit.

This code should be placed in your themes functions.php file.

add_filter( 'wp_nav_menu_items', 'wcv_vendors_menu', 10, 2 );
function wcv_vendors_menu ( $items, $args ) {
         if ($args->theme_location == 'primary') {
                $vendors  = get_users( array( 'role' => 'vendor' ) );
                $items .= '<li><a href="#">Vendors</a>';
                $items .= '<ul class="sub-menu">';
                foreach( $vendors as $vendor ) {
                        $store_url = WCVendors_Pro_Vendor_Controller::get_vendor_store_url($vendor->ID);
                        $store_id = WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor->ID );
                        $store_name = get_post_field( 'post_title', $store_id );
                        $items .= '<li><a href="'.$store_url.'">'.$store_name.'</a></li>';
                }
                $items .= '</ul></li>';
        }
    return $items;
}

 

filters

Updated on October 17, 2016

Was this article helpful?