我在这方面是个新手。到目前为止我所做的就是。我使用以下代码生成了一个包含所有第一级类别的元素:
<?php
$args = array(
'show_option_all' => 'Blog Categories',
'show_option_none' => '',
'option_none_value' => '-1',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 0,
'child_of' => 0,
'exclude' => '',
'include' => '4,2,3,5,6',
'echo' => 1,
'selected' => 0,
'hierarchical' => 1,
'name' => 'cat',
'id' => 'cat-blog',
'class' => 'form-control mt33',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'value_field' => 'term_id',
);
wp_dropdown_categories( $args );
?>但不幸的是,我无法设法将<a href="">添加到由wp_dropdown_categories函数生成的所有<option>标记中。
是否可以为每个生成的类别添加链接。我想将用户重定向到选定的类别页面。
提前谢谢你,亚历克斯
发布于 2017-04-18 21:11:37
你可以试试这段代码...
<select name="event-dropdown">
<option value=""><?php echo esc_attr_e( 'Blog Categories', 'textdomain' ); ?></option>
<?php
$args = array(
'orderby' => 'ID',
'order'=> 'ASC',
'exclude' => array(1)
);
$categories = get_categories($args);
foreach ( $categories as $category ) {
$term_link = get_category_link($category->term_id );
$term_link = esc_url( $term_link );
echo '<option value="'.$category->term_id.'"><a href="'.$term_link.'">'.$category->cat_name.'</a></option>';
}
?>
</select>https://stackoverflow.com/questions/43472134
复制相似问题