admin管理员组文章数量:1335363
I need help with adding a second category filter to a site. I have the first one working but not sure how to add a second one. The first one will choose the type of podcasts and the second needs to choose by a name. I already have the post types and categories setup in the functions.php file for the podcasts categories so would I need to add anything additional there since it will be using the same categories just filtering them in a different way?
And would I need to set them up as a parent category with child categories for each filter? For example one parent name with the Type of Podcasts to filter and one with the Names to filter?
Also how do I specify each filter to show just one with types in the dropdown and one with just names in the second dropdown?
<?php
$filter = get_query_var( 'podcast_category_filter' );
$args = array(
'taxonomy' => 'podcast_category',
'selected' => sanitize_text_field( $filter ),
'name' => 'podcast_category_filter',
'hide_empty' => false,
'orderby' => 'name',
'value_field' => 'slug',
'echo' => false,
'show_option_all' => 'All podcasts',
'option_none_value' => 0
);
$select_category_html = wp_dropdown_categories( $args );
?>
<form method='GET'>
<?php echo $select_category_html ?>
<button type='submit'>Filter</button>
</form>
<div class="podcasts">
<ul>
<?php $tax_query = array();
if ( ( 0 != $filter ) && is_string( $filter ) && ( '' != $filter ) ) {
$tax_query = array(
array(
'taxonomy' => 'podcast_category',
'field' => 'slug',
'terms' => sanitize_text_field( $filter ),
)
);
}
// Create a secondary query to avoid using the main query.
$args = array_merge(
array( 'post_type' => 'podcast' ),
array( 'tax_query' => $tax_query )
);
$podcast_query = new WP_Query( $args );
if ( $podcast_query->have_posts() ) :
while ( $podcast_query->have_posts() ) : $podcast_query->the_post(); ?>
<li>
</li>
</ul>
</div>
I appreciate any help! Thank you!
I need help with adding a second category filter to a site. I have the first one working but not sure how to add a second one. The first one will choose the type of podcasts and the second needs to choose by a name. I already have the post types and categories setup in the functions.php file for the podcasts categories so would I need to add anything additional there since it will be using the same categories just filtering them in a different way?
And would I need to set them up as a parent category with child categories for each filter? For example one parent name with the Type of Podcasts to filter and one with the Names to filter?
Also how do I specify each filter to show just one with types in the dropdown and one with just names in the second dropdown?
<?php
$filter = get_query_var( 'podcast_category_filter' );
$args = array(
'taxonomy' => 'podcast_category',
'selected' => sanitize_text_field( $filter ),
'name' => 'podcast_category_filter',
'hide_empty' => false,
'orderby' => 'name',
'value_field' => 'slug',
'echo' => false,
'show_option_all' => 'All podcasts',
'option_none_value' => 0
);
$select_category_html = wp_dropdown_categories( $args );
?>
<form method='GET'>
<?php echo $select_category_html ?>
<button type='submit'>Filter</button>
</form>
<div class="podcasts">
<ul>
<?php $tax_query = array();
if ( ( 0 != $filter ) && is_string( $filter ) && ( '' != $filter ) ) {
$tax_query = array(
array(
'taxonomy' => 'podcast_category',
'field' => 'slug',
'terms' => sanitize_text_field( $filter ),
)
);
}
// Create a secondary query to avoid using the main query.
$args = array_merge(
array( 'post_type' => 'podcast' ),
array( 'tax_query' => $tax_query )
);
$podcast_query = new WP_Query( $args );
if ( $podcast_query->have_posts() ) :
while ( $podcast_query->have_posts() ) : $podcast_query->the_post(); ?>
<li>
</li>
</ul>
</div>
I appreciate any help! Thank you!
Share Improve this question asked Nov 20, 2024 at 18:00 PeggyPeggy 671 silver badge13 bronze badges 4 |1 Answer
Reset to default 0I figured it out. I added parent and child categories and then added another Query but included parent => cat_ID to each array and added the parent ID's and it worked.
本文标签: phpI need to add a second category filterStack Overflow
版权声明:本文标题:php - I need to add a second category filter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339132a2456238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
podcast_category
items? Then you will have to work with parent and child categories, yes, because otherwise how would you differentiate between them in the first place? – C3roe Commented Nov 21, 2024 at 8:25