2016-03-04 91 views
2

试图将产品搜索栏添加到Wordpress管理栏后端,将执行Woocommerce产品搜索。它将位于后台管理菜单栏顶部,以便无论您在后端的哪个位置都可以搜索到woo的产品。我很接近,但在小绊脚石处发生了故障。尝试使用搜索时,它默认发布搜索而不是产品。试图添加搜索栏管理面板菜单栏搜索woocommerce产品

//Add Search To Admin Bar 
function boatparts_admin_bar_form() { 
global $wp_admin_bar; 
$wp_admin_bar->add_menu(array(
    'id' => 'boatparts_admin_bar_form', 
    'parent' => 'top-secondary', 
    'title' => '<form method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=product"> 
<input name="s" type="text" style="height:20px;margin:5px 0;line-height:1em;"/> 
<input type="submit" style="height:18px;vertical-align:top;margin:5px 0;padding:0 2px;" value="Search Products"/> 
</form>' 
)); 
} 
add_action('admin_bar_menu', 'boatparts_admin_bar_form'); 

有它在我的孩子主题的function.php。试图弄清楚它让我疯狂。

+0

喜Lucky_Hunter – Joe

+0

你好幸运猎人,感谢您的回复。完全按照我所希望的那样工作。我现在看到我缺少的东西。这是巨大的。再次谢谢你!!!!! – Joe

回答

2

你应该添加隐藏字段型后参数:

<input name="post_type" value="product" type="hidden"> 

此外,我添加一些代码为形式显示搜索查询表格后提交和一个小补丁的按钮样式。

固定码如下片段:

//Add Search To Admin Bar 
function boatparts_admin_bar_form() { 
    global $wp_admin_bar; 

    $search_query = ''; 
    if ($_GET['post_type'] == 'product') { 
    $search_query = $_GET['s']; 
    } 

    $wp_admin_bar->add_menu(array(
    'id' => 'boatparts_admin_bar_form', 
    'parent' => 'top-secondary', 
    'title' => '<form method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=product"> 
     <input name="s" type="text" value="' . $search_query . '" style="height:20px;margin:5px 0;line-height:1em;"/> 
     <input type="submit" style="padding:3px 7px;line-height:1" value="Search Products"/> 
     <input name="post_type" value="product" type="hidden"> 
    </form>' 
)); 
} 
add_action('admin_bar_menu', 'boatparts_admin_bar_form'); 

搜索结果示例:

Search results sample