admin管理员组文章数量:1122846
I am working for a client that has requested a drop down list for some categories within a page, i.e. click on the link and a list drops down to select the page you want to view. Everything I seem to be finding is telling me how to create the drop down in the nav bar, that is not what I need. Any help is greatly appreciated...
I am working for a client that has requested a drop down list for some categories within a page, i.e. click on the link and a list drops down to select the page you want to view. Everything I seem to be finding is telling me how to create the drop down in the nav bar, that is not what I need. Any help is greatly appreciated...
Share Improve this question asked Feb 8, 2015 at 21:49 Ben BurtBen Burt 12 bronze badges 1- You want add in header navbar – HK89 Commented Jan 1, 2021 at 8:12
1 Answer
Reset to default 0I'm not sure if you have pages with categories, but I can show you a example using posts instead.
Dropdown Filter from posts by category:
<?php
$url = get_home_url();
$category_slug = 'my-new-category'; //remember to get the slug
$posts_by_category = new WP_Query([
'post_type' => 'post',
'category_name' => $category_slug,
'nopaging' => true
]);
if ( $posts_by_category->have_posts() ) :
$dropdown = '<select onchange="window.location.href = this.value" name="posts-by-category">';
while ( $posts_by_category->have_posts ) : $posts_by_category->the_post();
$dropdown .= sprintf('<option value="%s?p=%s">%s</option>', $url, get_the_ID(), get_the_title() );
endwhile;
$dropdown .= '</select>';
endif; wp_reset_postdata();
echo $dropdown;
?>
本文标签: dropdownCreate a drop down list within an active page not nav menu
版权声明:本文标题:dropdown - Create a drop down list within an active page not nav menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284823a1927331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论