如何在WordPress中根据自定义分类过滤归档帖子
我创建了一个带有自定义分类的自定义帖子类型。为此,我在functions.php
//Custom Posttype
function custom_post_type_rental() {
// source: https://wordpress.stackexchange.com/questions/156978/custom-post-type-single-page-returns-404-error
$labels = array(
'name' => _x( 'Rentals', 'i2sa'),
'singular_name' => _x( 'rentals', 'i2sa'),
'menu_name' => _x( 'Rentals', 'i2sa'),
'name_admin_bar' => _x( 'Admin Bar', 'i2sa'),
'add_new' => _x( 'Nieuwe rental', 'i2sa'),
'add_new_item' => __( 'Voeg nieuwe rental toe', 'i2sa'),
'new_item' => __( 'Nieuw item', 'i2sa'),
'edit_item' => __( 'Bewerk rentals', 'i2sa'),
'view_item' => __( 'Bekijk rentals', 'i2sa'),
'all_items' => __( 'Alle rentals', 'i2sa'),
'search_items' => __( 'Zoek rentals', 'i2sa'),
'parent_item_colon' => __( 'Parent rentals:', 'i2sa'),
'not_found' => __( 'Zoekopdracht niet gevonden.', 'i2sa'),
'not_found_in_trash' => __( 'Zoekopdracht niet gevonden in prullenbak.', 'i2sa')
);
$args = array(
'labels' => $labels,
'description' => "Beschrijving i2sa",
'public' => true, //openbaar
'exclude_from_search'=> true, //uitschakelen voor zoekopdrachten
'publicly_queryable' => true, //publiekelijk vindbaar
'show_in_nav_menus' => false, //toon in navigatie
'menu_icon' => 'dashicons-bank',
'show_ui' => true,
'show_in_rest' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'rental' ), //custom url
'capability_type' => 'post',
'has_archive' => true, //heeft archivepage
'hierarchical' => false, // true maakt het een pagina
'menu_position' => null, //null = geen
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author', 'trackbacks', 'comments', 'revisions', 'post-formats', 'page-attributes' ), //gesupporte functionaliteiten
//je kan de support 'thumbnail' ook vervangen door ‘attachment:audio’ and ‘attachment:video’
);
register_post_type( 'rental', $args );
//flush_rewrite_rules();
}
add_action( 'init', 'custom_post_type_rental' );
function rentalMachineSoort() {
//Labels voor de custom taxonomy
$labels = array(
'name' => _x( 'Machinesoorten', 'machinesoorten' ),
'singular_name' => _x( 'Machinesoort', 'machinesoort' ),
'search_items' => __( 'Search Machinesoorten' ),
'all_items' => __( 'All Machinesoorten' ),
'parent_item' => __( 'Parent Machinesoort' ),
'parent_item_colon' => __( 'Parent Machinesoort:' ),
'edit_item' => __( 'Edit Machinesoort' ),
'update_item' => __( 'Update Machinesoort' ),
'add_new_item' => __( 'Add New Machinesoort' ),
'new_item_name' => __( 'New Machinesoort Name' ),
'menu_name' => __( 'Machinesoorten' ),
);
// Taxonomy registreren
register_taxonomy('machinesoort',array('rental'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'machinesoort' ),
));
}
add_action( 'init', 'rentalMachineSoort', 0 );
在我的存档中,我希望按此分类过滤帖子。为此,我使用代码:
<form method="GET">
<select name="orderby" id="orderby">
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value="<?php echo $term->slug;?>" name="machinesoort[]"><?php echo $term->name;?></option>
<?php endforeach;?>
</select>
<button type="submit">Filter</button>
这将使用包含参数的新URL刷新页面。问题是页面仍然显示所有文章,包括不应该显示的文章。
有人知道我怎样才能让这段代码正常工作,这样它就可以过滤了吗?如果我只使用CheckBox,它可以正常工作,但我需要在此筛选器中使用方法
解决方案
问题是页面仍然显示所有文章,包括不应该显示的文章。
因为您错过了主要步骤!您没有根据所选值更改查询!
您可以使用pre_get_posts
钩子来完成。如下所示:
add_action('pre_get_posts', 'altering_rental_archive_query', 99);
function altering_rental_archive_query($query)
{
if (
is_post_type_archive('rental')
&&
get_query_var('orderby')
)
{
$tax_query = array(
array(
'taxonomy' => 'machinesoort',
'field' => 'slug',
'terms' => sanitize_text_field(get_query_var('orderby')),
)
);
$query->set('tax_query', $tax_query);
};
};
结果如下:
在没有任何筛选器/查询变量的情况下首次加载存档页面时:
如果我按第一个自定义分类对其进行筛选:
如果我按第二个自定义分类对其进行筛选:
一个快速提示/奖励:
您可以将selected
Docs函数添加到archive
页面上的表单中,以便根据url:
<form method='GET'>
<select name='orderby' id='orderby'>
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'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>
您的问题中还缺少结束表单标记!</form>
。
相关文章