Php 对子类别使用不同的模板
我在WordPress中有一个父类别页面,显示一篇文章和下面的四个子类别。这四个子类别页面将共享相同的HTML和逻辑,但与父类别不同。父类别使用category.php和category.twig来处理其HTML和内容的显示。请参阅下面的代码。如何告诉WordPress和Timber为父类别(slug:stories)的子类别使用category-child.php和category-child.twig(示例名称)。我知道我可以对每个子slug或id使用category-slug.php或category-id.php,但这需要将相同的代码添加到四个(或更多)不同的php和Twig文件中,这并不理想 category.phpPhp 对子类别使用不同的模板,php,wordpress,twig,wordpress-theming,timber,Php,Wordpress,Twig,Wordpress Theming,Timber,我在WordPress中有一个父类别页面,显示一篇文章和下面的四个子类别。这四个子类别页面将共享相同的HTML和逻辑,但与父类别不同。父类别使用category.php和category.twig来处理其HTML和内容的显示。请参阅下面的代码。如何告诉WordPress和Timber为父类别(slug:stories)的子类别使用category-child.php和category-child.twig(示例名称)。我知道我可以对每个子slug或id使用category-slug.php或ca
/**
* @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 );
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' );
}
分类。细枝
{% 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 %}
完整的父和子类别结构
家长:故事
儿童:露营美国、加拿大、美国和世界
有什么想法吗?作为解决方案,您可以为子类别创建模板文件,并通过functions.php包含模板 一步一步地 创建模板文件,例如template sub 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 );
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' );
}
将代码添加到functions.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 );
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' );
}
将逻辑保留在category.php中。添加如下内容(sudo代码): $category=get_queryed_object();//将为您提供当前的WP_术语 如果($category->parent==0){ }否则{
// child category
$templates = array( 'child-category.twig' );
$context['dataForChildCategory'] = array( ... );
}
木材::渲染($templates,$context) 谢谢你的密码。当子类别显示在如下URL中时,它确实起作用:domain.com/child-category,但当它显示为domain.com/parent-category/child-category时,找不到模板。有什么想法吗?你能详细说明你的分类结构吗?据我所知,您的结构是domain.com/stories-主要类别?和domain.com/stories/sub-category-sub-category?是的,这是正确的类别结构。我已经编辑了我的问题,以包括完整的类别结构。当我点击一个子类别链接时,WordPress会显示404页面而不是子类别页面。我没有阅读整个(大量)问题,但是,是否可能包含WordPress已经包含的同一文件中的两个文件中的一个?@JoelM当子类别直接包含在域名之后时,Andrii发布的代码是有效的。例如,domain.com/child-category。当它在domain.com/parent/child-category的正确结构中使用时,模板覆盖失败,显示404。我已经注释掉了函数文件中的代码,并将您建议的代码添加到category.php逻辑文件中,但仍然没有成功。404仍然为子类别页面加载。因此,这是一个永久性的问题,而不是父/子类别之间的区别。。。通常在这种情况下,人们使用模式(设置->永久链接):1。自定义结构:/%category%/%postname%/2。类别库:。(dot)3。保存设置希望,这将有帮助我已经在Permalinks配置中使用了这些设置。还有其他想法吗?刚刚发布了clean WP,添加了“家长”类别,两个孩子类别(孩子01,孩子02),并为每个类别添加了一篇帖子。Permalinks可以很好地处理模式:/category/parent/(对于家长)、/category/parent/child-1/和/category/parent/child-2/(对于儿童类别)。所以我想,在您的情况下,我会从clean安装返回默认设置(Permalink设置中为空的“Category base”)。。。除非您对permalinks中的“/category/”不满意。然后我建议为帖子创建自定义分类法。您可以在register_taxonomy()函数中定义重写模式将永久链接设置恢复为默认设置已解决此问题。我不喜欢在我的链接中有“分类”,所以我将研究自定义分类法。谢谢你的提示。