我正在开发一个项目,但我陷入了一件小事,这对于 PHP 专家来说非常小:D 但我不是
我正在尝试制作适用于选择的自定义分类法的下拉列表,转到该自定义分类法页面。
但是经过大量搜索后,我找到了解决方案,但没有执行操作来转到所选的自定义分类法
首先我发现
<?php wp_dropdown_categories( 'taxonomy=my_custom_taxonomy' ); ?>
我发现的第二个
function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
$terms = get_terms( $taxonomy );
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
}
print( '</select>' );
}
}
当我插入任何页面代码时可以使用它
<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy' ); ?>
Credit https://frankiejarrett.com/2011/09/create-a-dropdown-of-custom-taxonomies-in-wordpress-the-easy-way/
但我现在不知道如何让它发挥作用
希望你能帮助我找到解决方案,从上面的任何一个解决方案中我都可以做出选择并继续。
<小时/>提前致谢
可能的答案 - 1
我找到了可能的答案
<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php
$args = array(
'show_option_none' => __( 'Select category' ),
'show_count' => 1,
'orderby' => 'name',
'name' => 'cat',
'echo' => 0,
'taxonomy' => 'MyCustomTaxonomys',
'value_field' => 'slug'
);
?>
<?php $select = wp_dropdown_categories( $args ); ?>
<?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
<?php $select = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>
<?php echo $select; ?>
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
给我网址
www.website.com/?cat=xxx where xxx is my custom taxonomy
但我想要网址
www.website.com/cat/xxx where xxx is my custom taxonomy
这可能吗?
最佳答案
你
可以使用这样的东西:
<?php
function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
$terms = get_terms( $taxonomy );
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
foreach ( $terms as $term ) {
printf( '<option value="%s" data-location="%s">%s</option>', esc_attr( $term->slug ), get_term_link(term_id), esc_html( $term->name ) );
}
print( '</select>' );
}
}
?>
然后使用您的 JavaScript 代码根据属性 data-location
的值进行重定向
关于javascript - 使用 JavaScript 自定义分类下拉列表,无需提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46904897/