首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为分类法"root“创建主题模板

如何为分类法"root“创建主题模板
EN

WordPress Development用户
提问于 2018-08-26 16:10:37
回答 1查看 727关注 0票数 3

我只是为一个使用了很多分类法的项目提出了这个问题,我需要列出使用分类法本身的帖子,而不仅仅是术语。

我有一个名为“颜色”的分类法,所以术语可以是“红色”、“蓝色”等等,我们可以看到使用这些术语访问的所有帖子:

代码语言:javascript
复制
site.com/colors/red
site.com/colors/blue

I希望能够访问"site.com/colors“,并使用"Colors”分类法中的任何术语列出所有内容,也就是说,我想使用taxo而不仅仅是术语列出帖子。

对于一个小项目,我为一个带有定制查询的静态页面创建了一个页模板,这个查询就是这样做的,但是在当前的项目中,我有大约8个分类法,并且为每个分类单元创建静态页面似乎有点蹩脚。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-08-28 23:10:01

这里有一个基于单个taxonomy.php文件的替代方案。

总之,在taxonomy.php开头调用一个函数。该函数计算分类法url,并标识应用了哪些自定义分类法(如果有的话)。然后,它获取该分类法的术语ALL >(不仅仅是url中的),然后获取<#>ALL --这些术语引用的posts。此时,您可以添加代码来格式化自定义分类法文章,但是如果您希望OR (如果没有自定义分类法帖子),您可以让taxonomy.php像往常一样接管和格式化这些文章。在我的(测试)示例中,函数进入functions.php,但如果您愿意,可以将它放入插件中。

我创建了两个自定义分类法(“颜色”和“位置”)。为了出售测试,我在函数中将硬编码的论文放入一个数组中,但是有许多替代方法。我的意图是表明,这可以通过编程完成,而无需创建众所周知的‘101个分类法文件’。

我创建taxonomy.php的方法是创建一个空白文件,将其保存为'taxonomy.php‘,然后复制archive.php的内容。在我的测试中,我只打印了术语post的标题(以证明post信息已被检索),但您可以将其更改为任何您想要的内容。

在屏幕快照中,您可以看到url是“location/悉尼”。标准分类法页面显示屏幕底部的两个“悉尼”帖子,但该函数为location=“墨尔本”提供了一个额外的帖子(显示在页面顶部)。

我的自定义分类法数组(我的变量名为$myarrayoptions)只有两个自定义分类法(鼻涕虫是'color‘和'location'),但这可以随您的喜好而定。我做了一个假设:每个帖子只有一个自定义分类法。同样,每个帖子也有处理多个分类法的方法,但这足以用于演示目的。

这是我在functions.php中的函数。我在所有的“回声”语句中都留了下来进行调试--您可以在空闲时删除这些语句。

代码语言:javascript
复制
/*
 * Create function to set if custom taxonomy is set
*/
function checktax(){
    global $wp;
    //echo "TESTING - START";

    // create a simple array showing the name of all the referenced custom taxonomies
    $myarrayoptions = array('color','location');
    //echo "the custom taxonomies are ";print_r($myarrayoptions);echo "";  //DEBUG

    // get the url
    $myurl = home_url( $wp->request );
    //echo "The url is ".$myurl." ";  //DEBUG

    // Explode the url to get access to the taxonomy and terms
    $pageterms = explode('/', $myurl);
    //echo "the exploded components of the URL ";print_r($pageterms);echo ""; //DEBUG

    // check whether any custom taxonomies are included in the page url and then do stuff
    if (array_intersect($pageterms,$myarrayoptions)) {

        // find which custom taxonomy is in the url - there should only be ONE!
        $result = array_intersect($pageterms,$myarrayoptions);
        //echo "the custom taxonomy is ";print_r($result);echo ""; //DEBUG

        // get the value of the custom taxonomy. this is very clumsy but it does work.
        foreach ($result as $key => $value){
        //echo "the array key is ".$key." and the taxonomy is ".$value."";  //DEBUG
        }
        // echo "the taxonomy is ".$value.""; //DEBUG

        // the term in the URL is the field after the taxonomy so we just add 1 to the key to get the term
        $urlterm = $pageterms[($key+1)];
        //echo "the term in the url is ".$urlterm.""; //DEBUG

        // get all the terms for this custom taxonomy
        $myterms = get_terms( array(
                'taxonomy' => $value,
                'hide_empty' => false,
        ) );    
        //echo "the terms are ";print_r($myterms);echo ""; //DEBUG

        //create a simple array to store the terms for use in a query
        $termsarray = []; 
        // get the slugs only
        $termsarray = wp_list_pluck( $myterms, 'slug' );
        //echo "terms array is ";print_r($termsarray);echo ""; //DEBUG

        // build a new query to get all the posts for the custom taxonomy
        // this is sortable but the OP didn't mention that
        $myargs = array(
                'post_type' => 'post',
                'posts_per_page' => -1,
                'tax_query' => array(
                        array(
                                'taxonomy' => $value,
                                'field' => 'slug',
                                'terms' => $termsarray,
                        )
                )
        );  
        $newquery = new WP_Query( $myargs );    
        //echo "newquery is ";print_r($newquery);echo "";  //DEBUG

        // now we have all the posts for the custom taxonomy
        // so we can print whatever we like

        // Page Header
        echo ''.$value.'';

        while ( $newquery->have_posts() ) {
            $newquery->the_post();
            the_title( '', '' );

        }

    }

        //echo "
TESTING - END
";  //DEBUG  
}

这是taxonomy.php的一个摘录。功能“支票税”做的工作。

代码语言:javascript
复制
get_header(); ?>



    
        
            ', '' );
                the_archive_description( '', '' );
            ?>
        
    最后,这是一个屏幕快照。
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/312545

复制
相关文章

相似问题

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