- This topic has 5 replies, 3 voices, and was last updated 4 years ago by
Leo.
-
AuthorPosts
-
May 13, 2019 at 2:47 am #897606
Ashley
Hi Guys,
I’m wanting to utilize the search in the navigation feature and want it to only search for woocommerce products.
I’ve used the below to move the search to the secondary menu and then followed this guide to utilise the WooCommerce search and add a placeholder.
Now, however, I’m getting duplicate search field appears when clicking on search and hitting ‘enter’ no longer does anything.
// Move Nav search to secondary menu
add_filter( ‘wp_nav_menu_items’, ‘tu_secondary_menu_search_icon’, 10, 2 );
function tu_secondary_menu_search_icon( $nav, $args ) {
$generate_settings = wp_parse_args(
get_option( ‘generate_settings’, array() ),
generate_get_defaults()
);if ( ‘enable’ !== $generate_settings[‘nav_search’] ) {
return $nav;
}if ( $args->theme_location == ‘secondary’ ) {
return $nav . ‘<li class=”search-item” title=”‘ . esc_attr_x( ‘Search’, ‘submit button’, ‘generatepress’ ) . ‘”><span class=”screen-reader-text”>’ . _x( ‘Search’, ‘submit button’, ‘generatepress’ ) . ‘</span>‘;
}return $nav;
}// Search For WooCommerce Products
add_filter( ‘generate_navigation_search_output’, function() {
printf(
‘<form method=”get” class=”search-form navigation-search” action=”%1$s”>
<input type=”search” class=”search-field” value=”%2$s” name=”s” title=”%3$s” />
<input type=”search” placeholder=”Search for a course and hit enter…” class=”search-field” value=”%2$s” name=”s” title=”%3$s” />
</form>’,
esc_url( home_url( ‘/’ ) ),
esc_attr( get_search_query() ),
esc_attr_x( ‘Search’, ‘label’, ‘generatepress’ )
);
} );May 13, 2019 at 6:34 am #897774David
StaffCustomer SupportHi there,
can you remove the second filter function that you’re using to change the text and search option and let us know if there is only one working search element in place?
May 13, 2019 at 6:41 am #897782Ashley
Hi David,
That seems to have removed the secondary search field and reinstated the functionality.
I’m still looking to have a palceholder and to only search WooCommerce products if you’re able to help with that at all?
May 13, 2019 at 7:26 am #897961Leo
StaffCustomer SupportCheck out this article for the 2 requests above:
https://docs.generatepress.com/article/generate_navigation_search_output/May 13, 2019 at 7:29 am #897966Ashley
Hi Leo,
If you refer to my original post you’ll see this is what I’ve tried to use already.
May 13, 2019 at 8:41 am #898045Leo
StaffCustomer SupportWeird. I just tried this snippet which takes care of moving the search to secondary navigation, only search for product and add a placeholder without any issues:
add_action( 'after_setup_theme', 'tu_move_nav_search' ); function tu_move_nav_search() { remove_action( 'generate_inside_navigation', 'generate_navigation_search' ); remove_action( 'generate_inside_navigation', 'generate_mobile_menu_search_icon' ); add_action( 'generate_inside_secondary_navigation', 'generate_navigation_search' ); add_action( 'generate_inside_secondary_navigation', 'generate_mobile_menu_search_icon' ); remove_filter( 'wp_nav_menu_items', 'generate_menu_search_icon', 10, 2 ); } add_filter( 'wp_nav_menu_items', 'tu_secondary_menu_search_icon', 10, 2 ); function tu_secondary_menu_search_icon( $nav, $args ) { $generate_settings = wp_parse_args( get_option( 'generate_settings', array() ), generate_get_defaults() ); if ( 'enable' !== $generate_settings['nav_search'] ) { return $nav; } if ( $args->theme_location == 'secondary' ) { return $nav . '<li class="search-item" title="' . esc_attr_x( 'Search', 'submit button', 'generatepress' ) . '"><a href="#"><span class="screen-reader-text">' . _x( 'Search', 'submit button', 'generatepress' ) . '</span></a></li>'; } return $nav; } add_filter( 'generate_navigation_search_output', function() { printf( '<form method="get" class="search-form navigation-search" action="%1$s"> <input type="search" placeholder="Enter your search" class="search-field" value="%2$s" name="s" title="%3$s" /> <input type="hidden" name="post_type" value="product" /> </form>', esc_url( home_url( '/' ) ), esc_attr( get_search_query() ), esc_attr_x( 'Search', 'label', 'generatepress' ) ); } );
Can you give it another shot?
-
AuthorPosts
- You must be logged in to reply to this topic.