我不确定您是否有带有类别的页面,但我可以向您展示一个使用帖子的示例。
按类别从帖子中下拉筛选:
<?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;
?>