Skip to content

Instantly share code, notes, and snippets.

@bentasm1
Forked from digitalchild/wcv_vendors_menu
Last active September 13, 2016 14:57
Show Gist options
  • Save bentasm1/403ef097aa8029587345 to your computer and use it in GitHub Desktop.
Save bentasm1/403ef097aa8029587345 to your computer and use it in GitHub Desktop.
WC Vendors Free & Pro Dynamic Vendor Menus
// Place this in your themes functions.php
// This will put the menu item in your primary menu. Change the theme location if you want to change which menu this goes in.
// Use the Free code for WC Vendors Free, use the Pro code for WC Vendors Pro
/* BEGIN WC Vendors Free */
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 ) {
$vendor_shop_link = site_url( WC_Vendors::$pv_options->get_option( 'vendor_shop_permalink' ) .$vendor->pv_shop_slug );
$items .= '<li><a href="'.$vendor_shop_link.'">'.$vendor->pv_shop_name.'</a></li>';
}
$items .= '</ul></li>';
}
return $items;
}
/* END WC Vendors Free */
/* BEGIN WC Vendors Pro */
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;
}
/* END WC Vendors Pro */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment