php - WordPress分类页面按月筛选当前分类文章
巴扎黑
巴扎黑 2017-04-10 14:41:39
[PHP讨论组]

在category.php页面使用了小工具,文章归档,就可以按月筛选文章。

问题:这个筛选是全站文章,怎么只筛选当前分类下的文章(不用小工具也可以,只要实现)

巴扎黑
巴扎黑

全部回复(2)
大家讲道理
    add_filter('getarchives_where','current_category_posts_where');

    function current_category_posts_where($where){
        if(!is_category())
            return;

        $category = get_the_category();
        $term_taxonomy_id = $category[0]->term_taxonomy_id;


        global $wpdb;
        return $where." AND {$wpdb->term_relationships}.term_taxonomy_id = {$term_taxonomy_id}";

    }

    add_filter('getarchives_join','current_category_posts_join');

    function current_category_posts_join(){
        if(!is_category())
            return;

        global $wpdb;
        return "INNER JOIN {$wpdb->term_relationships} on {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id";

    }

加到主题的functions.php即可.

作用:

访问category archive(in your case, it's category.php)页面时, 如http://www.test.com/category/uncategorized, 只会统计uncategorized下的posts.

阿神

是这个效果么。baixiaohei.sinaapp.com

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号