我找不到父级/子类别的 {%if categoryName%} 代码。
请查看我的屏幕截图和代码:
后端:
我对这些View atm的简码:
[events category="66"]
ShortcodeEvents.php文件:
<?php
namespace WpTheme\Shortcodes\Module;
use WpTheme\Shortcodes\ShortcodeModule;
class ShortcodeEvents extends ShortcodeModule
{
/**
* @param $atts
*
* @return mixed
*/
public function handle($atts, $content = null) {
extract(shortcode_atts([
'category' => false,
'limit' => - 1,
'parent' => $sub_category->cat_ID,
], $atts));
$args = [
'posts_per_page' => $limit,
];
if ($category) {
$category = collect(explode(',', $category))->map(function ($value) {
return trim($value);
})->filter()->toArray();
$args = collect($args)->merge([
'tax_query' => [
[
'taxonomy' => 'event-category',
'field' => 'term_id',
'terms' => $category,
],
],
])->toArray();
$events = app(\WpTheme\PostTypes\Repository\Event::class)->all($args);
$categoryName = get_term(array_shift($category), $taxonomy)->name;
} else {
$events = app(\WpTheme\PostTypes\Repository\Event::class)->allGroupedByCategory();
}
return $this->renderView('shortcodes/events.twig', compact('events', 'categoryName'), false);
}
}
我的树枝文件:
{% if events %}
{% if categoryName %}
<h2>{{ categoryName }}</h2>
<hr class="mt-0 mb-8">
{% endif %}
{% for parentTerm in events %}
<div class="{{ loop.index == 1 ? 'mb-default' : 'mt-default' }}">
<div class="mb-8 mb-lg-12 mb-xl-16">
<h2>{{ parentTerm.name }}</h2>
{% if parentTerm.description %}
<p>{{ parentTerm.description }}</p>
{% endif %}
</div>
{% for childTerm in parentTerm.childTerms %}
<div class="mb-8 mb-lg-12">
<h3>{{ childTerm.name }}</h3>
{% for event in childTerm.events %}
{% set index = random() %}
<div class="my-4">
<b-button variant="unstyled" v-b-toggle.accordion-{{ index }} class="accordion-trigger">
<span class="plus"></span>
<h4>{{ event.title }}</h4>
</b-button>
<b-collapse id="accordion-{{ index }}" accordion="my-accordion" role="tabpanel">
<div class="py-4 py-lg-6">
{% set speaker = event.meta('speaker') %}
{% if speaker %}
<p>
<a href="{{ speaker.link }}">{{ speaker.title }}</a>
</p>
{% endif %}
{% if event.meta('dates') %}
<table class="table-responsive-md">
<thead>
<tr>
<th>{{ function('trans', 'global.date') }}</th>
<th>{{ function('trans', 'global.place') }}</th>
<th>{{ function('trans', 'global.topic') }}</th>
</tr>
</thead>
<tbody>
{% for date in event.meta('dates') %}
<tr>
<td class="text-nowrap">{{ date.date|date("d.m.Y") }}</td>
<td class="text-nowrap">{{ date.location }}</td>
<td>{{ date.topic }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</b-collapse>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% endfor %}
{% endif %}
我发现,如果我使用,至少我可以从子类别中获得一些物品
{{parentterm.title}}而不是{{event.title}}
{%如果parentTerm.meta('dates')%}而不是{%如果event.meta('dates')%}
但是parentTerm.category或parentTerm.categoryName似乎不起作用。
有什么想法吗?
答案 0 :(得分:0)
@Alorsons我想确保您知道子类别/子术语自然通过Timber\Term
通过任何{{ term.children }}
对象出现-这将返回表示其他任何对象的其他Timber\Term
对象的数组子级(category
分类法的子类别-但可以与任何分层分类法一起使用)