是否有一个模板文件来列出给定定制分类的所有术语?

时间:2016-08-28 作者:James Nolan

假设我们注册了composers 以及interprets 自定义分类法。

这个composers 分类法可以有以下术语:

沃尔夫冈·阿马德乌斯·莫扎特(Wolfgang Amadeus Mozart)路德维希·范·贝多芬(Ludwig van Beethoven)的理查德·瓦格纳(Richard Wagner)interprets 分类法可以有以下术语:

我想知道WordPress模板层次结构是否有一个特殊的文件,如taxonomy-terms.php, 要使用简单循环列出分类术语,请执行以下操作:

<?php
while ( have_posts() ) : the_post();
    the_title();
endwhile;
?>
这将打印(假设/%postname%/ 永久链接设置):

  • Wolfgang Amadeus Mozart, Ludwig van Beethoven, Richard Wagner 查看时example.com/composers
  • Glenn Gould, Daniel Barenboim, Vienna State Opera 查看时example.com/interpretstemplate-composers-terms.php 和template-interprets-terms.php 并使用以下代码列出术语:

    <?php
    /* Template Name: List of composers */ // or \'List of interprets\'
    $terms = get_terms( \'composers\' ); // or get_terms( \'interprets\' )
    foreach ( $terms as $term ) :
        echo $term->name;
    endforeach;
    ?>
    
    然后,我创建了两个页面,“作曲家”和“解释”,我分别为其分配了“作曲家列表”和“解释列表”模板。

    我不太习惯使用这种方法,因为它需要为每个分类法创建一个新文件。

2 个回复
SO网友:mmm

您可以在中包含相同的文件template-composers-terms.phptemplate-interprets-terms.php 就像这样:

<?php
/* Template Name: List of composers */

$tax = "composer";
require "include/file_which_does_all_the_work.php";
如果页面slug与分类slug相同,则可以保存该行$tax = ... 包含在包含的文件中:

$tax = $GLOBALS["wp_query"]->query["pagename"];

SO网友:T3mmX

代码有错误,我想。。。因为get\\u术语支持数组作为参数<因此,我认为最好的方法是:
1-创建模板生成器术语。php
2-使用以下内容将其包含在页面中:

<?php get_template_part(\'template\',\'composers-terms\') ?>
最后,在新创建的文件中输入:

<?php
/* Template Name: List of composers or \'List of interprets\' */
$terms = get_terms(array(
         \'taxonomy\' => \'composers\', // make sure it is the taxonomy key passed at the  register_taxonomy function.
         \'hide_empty\' => false
 );
foreach ( $terms as $term ) :
    echo $term->name;
endforeach;
?>

相关推荐