【问题标题】:Use different template for children categories为子类别使用不同的模板
【发布时间】:2019-11-18 19:37:34
【问题描述】:

我在 WordPress 中有一个父类别页面,该页面显示一个帖子及其下方的四个子类别。四个子类别页面将共享相同的 HTML 和逻辑,但与父类别不同。父类别使用 category.php 和 category.twig 来处理其 HTML 和内容的显示。请参阅下面的代码。我如何告诉 WordPress 和 Timber 使用 category-child.php 和 category-child.twig (示例名称)作为父类别的孩子(slug:故事)。我知道我可以为每个子 slug 或 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' );
}

完整的父子类别结构

父级:故事 儿童:露营美国、加拿大、美国和世界。

有什么想法吗?

【问题讨论】:

    标签: php wordpress twig wordpress-theming timber


    【解决方案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' );
    }
    

    【讨论】:

    • 感谢您的代码。当子类别显示在这样的 URL 中时,它确实有效:domain.com/child-category,但是当它显示为 domain.com/parent-category/child-category ,找不到模板。有什么想法吗?
    • 您能否为您的类别指定结构?据我了解,您的结构是 domain.com/stories - 主要类别?还有 domain.com/stories/sub-category - 子类别?
    • 是的,这是正确的类别结构。我已经编辑了我的问题以包含完整的类别结构。当我单击子类别链接时,WordPress 会显示 404 页面而不是子类别页面。
    • 我没有阅读整个(大量)问题,但是否可以从 WordPress 已经包含的同一文件中包含 2 个文件之一?
    • @JoelM 当子类别直接包含在域名之后时,Andrii 发布的代码有效。例如,domain.com/child-category。在 domain.com/parent/child-category 的正确结构中使用时,模板覆盖无法正常工作并显示 404。
    【解决方案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);

    【讨论】:

    • 我已经注释掉了functions文件中的代码,并将您建议的代码添加到category.php逻辑文件中,但仍然没有成功。仍然为子类别页面加载 404。
    • 因此,与其区分父/子,不如说是永久链接问题...通常在这种情况下人们使用模式(设置 -> 永久链接): 1. 自定义结构:/%category%/%postname% / 2. 分类库:. (dot) 3. 保存设置希望,会有所帮助
    • 我已经在我的永久链接配置中使用了这些设置。还有其他想法吗?
    • 刚刚启动了干净的 WP,添加了“父母”类别,两个儿童类别(儿童 01,儿童 02)并为每个类别添加了一个帖子。永久链接适用于以下模式:/category/parent/(对于 Parent)、/category/parent/child-1/ 和 /category/parent/child-2/(对于子类别)。所以我想,在你的情况下,我会从全新安装返回默认设置(永久链接设置中的“类别库”为空)......除非你对永久链接中的“/类别/”不满意。然后我建议为帖子创建自定义分类法。您可以在 register_taxonomy() 函数中定义重写模式
    • 将永久链接设置恢复为默认值已解决了该问题。我不喜欢在链接中包含“类别”,因此我将研究自定义分类法。感谢您的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2019-10-24
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多