分类模板...按层级?

时间:2015-11-23 作者:Will

是否有一种方法可以在由文件名指定的层次分类法中为不同的级别使用不同的模板。我知道taxonomy-taxonomyname.phptaxonomy-taxonomyname-term.php 模板。正在寻找一种更通用的基于“每分类级别”的方法。

因此,命名为Earth 可能包含:

非洲喀麦隆刚果塞内加尔亚洲日本韩国欧洲希腊earth-numbers-level.php)... 喀麦隆、刚果、日本、希腊的另一个模板。。。(earth-bullets-level.php)?

查看一些不同的模板层次结构文档,我猜不是。如果这是真的,what\'s the best/most efficient way to differentiate taxonomy levels within taxonomy-earth.php 这样我就可以为每个模板使用不同的模板部分了?

2 个回复
最合适的回答,由SO网友:gmazzap 整理而成

+我听了彼得·古森(PieterGoosen)的回答,但这次我想成为提倡“简单化”方式的人。

在您的taxonomy-earth.php 模板,您可以检查查询的术语是否为父项,如果是,则需要另一个模板。

如果你问这个问题,我想你已经有(或希望创建)2个模板了,让我们称它们为:

  • taxonomy-earth-continent.php
  • taxonomy-earth-country.phptaxonomy-earth.php 文件您可以:

    <?php
    $slug = \'taxonomy-earth\';
    // default to continent
    $name = \'continent\';
    
    // maybe set specific template to "country" if queried term has a parent
    $term = get_queried_object();
    if ( is_object( $term ) && ! empty( $term->parent ) )
        $name = \'country\';
    
    get_template_part( $slug, $name );
    
    这6行模板代码足以要求“子”术语使用不同的模板。

    由于使用了get_template_part 模板代码也对子主题友好,并触发挂钩:get_template_part_taxonomy-earth 当需要模板时,可以使用它来做事情。

SO网友:Pieter Goosen

您在这里有几个选项:

taxonomy-{$taxonomy}-{$term->parent}-{$term}.php

我们可以通过创建自己的层次结构来创建自己的层次结构(或实际扩展现有的层次结构taxonomy-{$taxonomy}-{$term->parent}-{$term}.php 查看子术语时使用的模板。我们还将利用taxonomy_template 筛选以将新的分类法模板添加到层次结构中,以便使用它们。

您可以尝试以下操作:(NOTE: 所有代码都未经测试,并对所有代码进行了注释,以便于后续操作和理解。代码还假设顶级术语已经具有taxonomy-{$taxonomy}-{$term}.php 模板

add_filter( \'taxonomy_template\', function ( $template )
{
    // Get the current term object being viewed
    $current_term = get_queried_object();

    // We can restrict this to a single taxonomy, for example
    // if ( $current_term->taxonomy !== \'my_taxonomy\' )
        // return $template;

    /**
     * Check if current term is top level, if so, return the default $template which
     * should be template taxonomy-{$taxonomy}-{$term}.php if found
     */
    if ( $current_term->parent == 0 ) // Top level terms have parent of 0
        return $template;

    // We made it to here, so the term is not top level

    // We need to get the top level term of the current term
    $hierarchy = get_ancestors( $current_term->term_id, $current_term->taxonomy );
    // The parent ID will always be the last ID in the array returned by get_ancestors
    $parent_ID = end( $hierarchy );
    // Now we can get the top level term object
    $top_level_term = get_term_by( \'id\', $parent_ID, $current_term->taxonomy );

    /** 
     * Lets build our custom template name, add subfolder name if template
     * is in a subfolder, for example /subfolder/name-of-template.php
     */
    $custom_template = \'taxonomy-{$current_term->taxonomy}-{$top_level_term->slug}-{$current_term->slug}.php\';
    // Check if our custom template exist, if not, return default $template
    $locate_template = locate_template( $custom_template );
    if ( !$locate_template )
        return $template;

    // Finally, everything checked out, return our custom template
    return $template = $locate_template;
}); 
您可以根据需要调整代码

自定义模板零件可以创建taxonomy-{$taxonomy}-{$term}.php 模板和利用循环中的模板部分,根据子术语包括模板部分。这里最好的方法是编写一个自定义函数,然后在循环内调用该函数(或任何需要它的地方),而不是get_template_part()

为此,我们需要调用模板部分,如下所示:

顶级术语将为{$term}。php,比如africa.php

子术语将是{$term->parent}-{$term}.php 喜欢africa-cameroon.php

默认/回退模板content.php. 记住不要通过.php 函数模板名称的一部分

这是代码

/** 
 * Function to set template parts according to child term
 *
 * @param (string) $default Default template part to use like content.php
 * @return $template
 */
function get_custom_template_part( $default = \'\' )
{
    // Check if we have a value for $default, if not, return false
    if ( !$default )
        return false;

    // Sanitize the $default value
    $default = filter_var( $default, FILTER_SANITIZE_STRING );

    // Check if we are on a taxonomy page, if not, return the $default template
    if ( !is_tax() )
        return get_template_part( $default );

    // Get the current term being viewed
    $current_term = get_queried_object();

    /**
     * Set our custom variables
     * $top_level_term will hold the top level term object
     * $part will hold the current term slug if the current term is not top level
     */
    $top_level_term = \'\';
    $part = \'\';

    // Check if current term is top level, if not, get the top level parent
    if ( $current_term->parent != 0 ) {
        // We need to get the top level term of the current term
        $hierarchy = get_ancestors( $current_term->term_id, $current_term->taxonomy );
        // The parent ID will always be the last ID in the array returned by get_ancestors
        $parent_ID = end( $hierarchy );
        // Now we can get the top level term object
        $top_level_term = get_term_by( \'id\', $parent_ID, $current_term->taxonomy );
        $part = $current_term->slug;
    }

    // We now will set our template\'s name accordingly
    if ( $top_level_term ) {
        $name = $top_level_term->slug;
    } else {
        $name = $current_term->slug;
    }

    // We will now check if our template parts exist, if not, return our default
    if ( $part ) { // This means we have a child term
        $template = get_template_part( $name, $part );
    } else { // This means top level term
        $template = get_template_part( $name );
    }

    if ( $template )
        return $template;

    return get_template_part( $default );
}
您可以根据需要对此进行扩展,也可以在子文件夹中添加模板部分,然后只需将子文件夹名称附加到$name 函数内部

现在,您可以在taxonomy-{$taxonomy}-{$term}.phptaxonomy-{$taxonomy}.php 样板

默认模板:content.php

get_custom_template_part( \'content\' );

相关推荐

Templates for Mobile Site

是否有任何内置方法可以根据浏览器大小显示不同的模板(即移动设备检测)?我做了一些研究,我能找到的只是大量插件,它们的功能远远超出了我的需要。我基本上只需要一种方法,将移动目录添加到我的主题中,并为移动用户显示该主题。