我有一个定制的播客帖子类型,但我无法让类别过滤器工作.我创建了名为Podcast Categories的定制分类.当我点击其中一个类别时,它们仍然显示.每个添加的播客都有不同的类别.我想不出该怎么做,所以如果有任何帮助,我将不胜感激!
这是我的unctions.php文件-
//Podcasts
function podcast_custom_post_type () {
$labels = array (
'name' => 'Podcasts',
'singular_name' => 'Podcast',
'add_new' => 'Add New Podcast',
'all_items' => 'All Podcasts',
'add_new_item' => 'Add A Podcast',
'edit_item' => 'Edit Podcast',
'new_item' => 'New Podcast',
'view_item' => 'View Podcast',
'parent_item_colon' => 'Parent Item',
'rewrite' => array( 'slug' => 'podcast' )
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_icon' => 'dashicons-admin-users',
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'custom-fields',
'revisions',
'page-attributes'
),
//'taxonomies' => array('category', 'post_tag'),
'menu_position' => 10,
'exclude_from_search' => false
);
register_post_type('podcast', $args);
}
add_action('init', 'podcast_custom_post_type');
function podcast_custom_taxonomies() {
$labels = array(
'name' => 'Podcast Categories',
'singular_name' => 'Podcast Category',
'search_items' => 'Search Podcast Categories',
'all_items' => 'All Podcast Category',
'parent_item' => 'Parent Podcast Category',
'parent_item_colon' => 'Parent Podcast Category:',
'edit_item' => 'Edit Podcast Category',
'update_item' => 'Update Podcast Category',
'add_new_item' => 'Add New Podcast Category',
'new_item_name' => 'New Podcast Category',
'menu_name' => 'Podcast Categories'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'podcast_category' )
);
register_taxonomy('podcast_category', array('podcast'), $args);
}
add_action( 'init' , 'podcast_custom_taxonomies' );
add_action('pre_get_posts', 'altering_podcast_archive_query', 99);
function altering_podcast_archive_query($query)
{
if (
is_post_type_archive('podcast')
&&
get_query_var('orderby')
)
{
$tax_query = array(
array(
'taxonomy' => 'podcast_category',
'field' => 'slug',
'terms' => sanitize_text_field(get_query_var('orderby')),
)
);
$query->set('tax_query', $tax_query);
};
};
这是我创建的主题页面-
<?php
/*Template Name: News & Media */
get_header();
?>
<div class="singlewidth">
<div id="primary">
<form method='GET'>
<select name='orderby' id='orderby'>
<?php
$terms = get_terms([
'taxonomy' => 'podcast_category',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value='<?php echo $term->slug; ?>' <?php echo selected(sanitize_text_field($_GET['orderby']), $term->slug); ?>><?php echo $term->name; ?></option>
<?php endforeach; ?>
</select>
<button type='submit'>Filter</button>
</form>
<div class="podcasts">
<ul>
<?php
query_posts(array(
'post_type' => 'podcast'
)); ?>
<?php
while (have_posts()) : the_post(); ?>
<li>
<?php if ( has_post_thumbnail() ) { /* loades the post's featured thumbnail, requires Wordpress 3.0+ */ echo '<div class="featured-thumb clearfix">'; the_post_thumbnail(); echo '</div>'; } ?>
<div class="podcasttext">
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'azurebasic' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php if( get_field('podcast_text') ): ?>
<?php the_field('podcast_text'); ?>
<?php endif; ?>
</div>
<div class="podcastplayer">
<?php if( get_field('add_podcast') ): ?>
<?php the_field('add_podcast'); ?>
<?php endif; ?>
</div>
</li>
<?php
endwhile;
?>
</ul>
</div>
</div>
</div>
<?php get_footer(); ?>
这是它所在的网页- https://baptist.tfm-dev.com/resources/news-resources个