我想在单击下拉选择项时显示帖子
目前,我的选择是确定的,显示所有我的条款和所有帖子。
我只是想知道是否可以过滤
这是我的下拉选择:
<select name="soins-taxonomy">
<option value="all">Tout afficher</option>
<?php
// Get the taxonomy\'s terms
$terms = get_terms(
array(
\'taxonomy\' => \'location\',
\'hide_empty\' => false,
\'exclude\' => 1
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) { ?>
<?php echo \'<option value="\' . $term->term_id . \'">\' . $term->name . \'</option>\';
}
}
?>
</select>
这是我用来显示帖子的查询:
<?php
$ourCurrentPage = get_query_var(\'paged\');
$args = array(
\'order\' => \'ASC\',
\'post_type\' => \'etablissements\',
\'taxonomy\' => \'location\',
\'posts_per_page\' => 9,
\'paged\' => $ourCurrentPage
);
// Custom query.
$query_loc = new WP_Query( $args );
// Check that we have query results.
if ( $query_loc->have_posts() ) {
// Start looping over the query results.
while ( $query_loc->have_posts() ) {
$query_loc->the_post();?>
<!-- START ARTICLE -->
<div class="col-xl-4 col-lg-6 col-md-6 mb-30">
<div class="single-etablissement">
<p class="single-etablissement-title"><?php the_title(); ?></p>
<p><?php the_field(\'eta_adress\'); ?></p>
<p><?php the_field(\'eta_cp\'); ?> - <?php the_field(\'eta_city\'); ?></p>
<p><?php the_field(\'eta_country\'); ?></p>
</div>
</div>
<!-- END ARTICLE -->
<?php
} // End while
} // End if
else { echo \'<p>Aucune actualité trouvée</p>\'; } ?>
<?php wp_reset_postdata(); ?>
SO网友:Qaisar Feroz
您可以使用tax_query
对于Taxonomy Parameters 为您的WP_Query.
$term_id = $_REQUEST [\'soins-taxonomy\']; // or use $_POST or $_GET as the case may be
$args = array(
\'order\' => \'ASC\',
\'post_type\' => \'etablissements\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'location\',
\'field\' => \'term_id\',
\'terms\' => $term_id,
),
\'posts_per_page\' => 9,
\'paged\' => $ourCurrentPage
);
我希望这会有所帮助。