首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在wp_get_archives()中使用自定义分类法

在wp_get_archives()中使用自定义分类法
EN

Stack Overflow用户
提问于 2021-03-24 15:26:00
回答 1查看 170关注 0票数 0

我正在使用下面的代码来获得我的帖子的年度档案,我的帖子类型是'foi‘。

这是我的代码:

代码语言:javascript
运行
复制
wp_get_archives(
    array(
        'post_type'       => 'foi',
        'type'            => 'yearly',
        'limit'           => '10',
        'show_post_count' => 'true'
    )
);

我为post类型的foi设置了分类法,我想在wp_get_archives()函数中以某种方式使用它。一个例子是显示具有post类型的、foi、以及document分类法的所有帖子的年度存档。

如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2021-03-24 15:44:44

您可以使用如下所示的过滤器:

在模板/自定义存档-template.php中

代码语言:javascript
运行
复制
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中:

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66784239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档