为什么搜索结果会给出产品ID而不是名称?
我在Woocommerce中为witters做了分类,并希望过滤所有witers并从选定的写手中显示书籍。
这是分类法:
<pre>if (!function_exists('bookcover_books_taxonomy')) {
function bookcover_books_taxonomy() {
// Writters
$labels = array(
'name' => _x( 'Writters', 'taxonomy general name' ),
'singular_name' => _x( 'Writter', 'taxonomy singular name' ),
'search_items' => __( 'Search writters' ),
'popular_items' => __( 'Popular writters' ),
'all_items' => __( 'All writters' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit writter' ),
'update_item' => __( 'Update writter' ),
'add_new_item' => __( 'Add new writter' ),
'new_item_name' => __( 'New writter name' ),
'separate_items_with_commas' => __( 'Separate writters with commas' ),
'add_or_remove_items' => __( 'Add or remove writter' ),
'choose_from_most_used' => __( 'Choose from the most used writter' ),
'not_found' => __( 'No writter found.' ),
'menu_name' => __( 'Writters' )
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'writter')
);
register_taxonomy( 'writter', 'product', $args );
}
add_action('init', 'bookcover_books_taxonomy', 0);
}</pre>
这是搜索表单:
<form role="search" method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>">
<label class="screen-reader-text" for="s"><?php _e('Search for books', 'woocommerce'); ?></label>
<?php
wp_dropdown_categories(array(
'show_option_all' => 'all categories',
'show_option_none' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'writter',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'writter',
'hide_if_empty' => false,
'walker' => ''
));
?>
<?php wp_list_categories() ?>
<input type="submit" id="searchsubmit" value="" />
<input type="hidden" name="post_type" value="product" />
当您从下拉列表中选择一个编写器并按搜索时,您将获得此URL
/?writter=27&post_type=product
问题出在第27号,我需要显示作者姓名,例如
/?writter=john-doe&post_type=product
谢谢!
答案 0 :(得分:0)
我认为此代码可能对您有所帮助。
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<label class="screen-reader-text" for="s"><?php _e('Search for books', 'woocommerce'); ?></label>
<?php
wp_dropdown_categories(array(
'show_option_all' => 'all categories',
'show_option_none' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'writter',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'writter',
'hide_if_empty' => false,
'walker' => ''
));
wp_list_categories() ?>
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="product" /> <!-- // hidden 'products' value -->
<input type="hidden" value="<?php echo $term->name; ?>" name="catname" />
<input type="submit" alt="Search" value="Search" />
</form>