个性化阅读
专注于IT技术分析

对子类别使用不同的模板

我在WordPress中有一个父类别页面, 该页面在其下方显示一个帖子和四个子类别。四个子类别页面将共享相同的HTML和逻辑, 但与父类别不同。父类别使用category.php和category.twig来处理其HTML和内容的显示。请参见下面的代码。我如何告诉WordPress和Timber为父类别(子弹:故事)的子代使用category-child.php和category-child.twig(示例名称)。我知道我可以为每个子段符或ID使用category-slug.php或category-id.php, 但这需要将相同的代码添加到四个(或更多)不同的PHP和Twig文件中, 这并不理想。

category.php

/**
 * @package  WordPress
 * @subpackage  gerryfeehan
 * @since   1.0.0
 */

$templates = array( 'category.twig', 'index.twig' );

$context = Timber::context();

$args = array(
    'cat' => '7, 5, 3, 4, 6', 'numberposts' => 1, 'orderby' => 'date', 'order' => 'DESC', );
$context['stories'] = Timber::get_posts($args);

$context['categories'] = Timber::get_terms('category');

$categories = get_categories(
    array(
        'hide_empty' => '0', 'parent'     => 7, 'orderby'    => 'id', 'order'      => 'ASC'
    )
);

// $context['categories'] = $categories;

// Updated code with suggestions from Tomek

$category = get_queried_object(); // will give you current WP_Term

if( $category->parent == 0 ) {
    // parent category
    $templates = array( 'category.twig' );
    $context['categories'] = $categories;
} else {
    // child category
    $templates = array( 'category-map.twig' );
    $context['categories'] = $categories;
}

// Timber::render( array( 'category.twig', 'index.twig' ), $context );
Timber::render( $templates, $context );

category.twig

{% extends "base.twig" %}

{% block content %}
<section class="stories">
    <h2>Stories</h2>
    {% for story in stories %} 
    <article class="story" id="story-{{ story.ID }}">
        {% if story.thumbnail.src %}
            <figure>
                <img src="{{ story.thumbnail.src }}" class="" alt="{{ story.thumbnail.alt }}" />
                {% if story.thumbnail.caption %}
                    <figcaption>{{ story.thumbnail.caption }}</figcaption>
                {% endif %}
            </figure>
        {% endif %} 
        <h3 class="story__heading">
            <a href="{{ story.link }}">
                {{ story.title }}
            </a>
        </h3>
        <div class="story__meta">
            <time class="">{{ story.date }}</time>
        </div>
        <div class="story__content">
            {{ story.preview.read_more(false) }}
        </div>
    </article>
    {% endfor %}
</section>
{% if function(cat_is_ancestor_of(7, 5)) %}yolo{% endif %}
{% for category in categories %}
<div class="category">
    <figure>
        <figcaption>
            {{ category.name }}
        </figcaption>
        {{ category.description }}
    </figure>
</div>
{% endfor %}
{% endblock %}

可以将以下代码添加到archive.php文件中, 但是我不确定如何将其扩展以满足我的需求。

else if ( is_category() ) {
    $term = new Timber\Term( get_queried_object_id() );

    $context['term']  = $term;
    $context['title'] = single_cat_title( '', false );

    array_unshift( $templates, 'archive-' . $term->slug . '.twig' );
}

完整的父级和子级类别结构

父母:故事孩子:美国, 加拿大, 美国和世界露营。

有任何想法吗?


#1


作为解决方案, 你可以为子类别创建模板文件, 并通过functions.php包含模板

循序渐进

创建你的模板文件, 例如template-sub-category.php

将代码添加到你的functions.php

add_filter( 'template_include', 'your_prefix_set_template', 10, 1 );

function your_prefix_set_template( $template_path ) {
    if ( ! is_category() ) {
        return $template_path;
    }

    $parent_category = get_term_by( 'slug', 'stories', 'category' ); // parent category

    if ( empty( $parent_category ) ) {
        return $template_path;
    }

    $term = get_queried_object();
    if ( $term->parent !== $parent_category->term_id ) { // check if is the parent category
        return $template_path;
    }

    return locate_template( 'template-sub-category.php' );
}

#2


将逻辑保留在category.php中。添加类似以下内容(sudo代码):

$ category = get_queried_object(); //将为你提供当前的WP_Term

if($ category-> parent == 0){

// parent category
$templates = array( 'parent-category.twig' );
$context['dataForParentCategory'] = array( ... );

}其他{

// child category
$templates = array( 'child-category.twig' );
$context['dataForChildCategory'] = array( ... );

}

Timber :: render($ templates, $ context);

赞(0)
未经允许不得转载:srcmini » 对子类别使用不同的模板

评论 抢沙发

评论前必须登录!