我正在使用下面的代码来获得我的帖子的年度档案,我的帖子类型是'foi‘。
这是我的代码:
wp_get_archives(
array(
'post_type' => 'foi',
'type' => 'yearly',
'limit' => '10',
'show_post_count' => 'true'
)
);我为post类型的foi设置了分类法,我想在wp_get_archives()函数中以某种方式使用它。一个例子是显示具有post类型的、foi、以及document分类法的所有帖子的年度存档。
如何才能做到这一点?
发布于 2021-03-24 15:44:44
您可以使用如下所示的过滤器:
在模板/自定义存档-template.php中
add_filter( 'getarchives_where', 'custom_archive_by_category_where' );
add_filter( 'getarchives_join', 'custom_archive_by_category_join' );
$args = array();
wp_get_archives(
array(
'type' => 'yearly',
'format' => 'option',
'post_type' => 'news',
)
);
remove_filter( 'getarchives_where', 'custom_archive_by_category_where' );
remove_filter( 'getarchives_join', 'custom_archive_by_category_join' );在functions.php中:
function custom_archive_by_category_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function custom_archive_by_category_where($x) {
global $wpdb;
$current_term_slug = get_query_var( 'news_category' );
if (!empty($current_term_slug)) {
$current_term = get_term_by('slug', $current_term_slug, 'news_category');
if (is_wp_error($current_term) ) {
return $x;
}
$current_term_id = $current_term->term_id;
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'news_category' AND $wpdb->term_taxonomy.term_id IN ($current_term_id)";
}
return $x;
}请将news_category更改为您的欲望类别段塞。最初发布在:https://developer.wordpress.org/reference/functions/wp_get_archives/#div-comment-3182上
https://stackoverflow.com/questions/66784239
复制相似问题