Timber Twig Show 与类别相关的帖子 [英] Timber Twig Show Posts Associated with Category

查看:40
本文介绍了Timber Twig Show 与类别相关的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Timber 和 Twig 的新手.我在 Timber 和 Twig 中使用了高级自定义字段.在自定义字段中,我有一个字段,允许用户通过分类字段类型使用 term 对象的返回值检查任何类别的 3 个.

我想要做的是显示用户选择的任何类别中的 3 个,以及下方该类别中的 3 个相关帖子.我总共有 14 个类别.

看起来像这样

在我的 PHP 文件中,我有

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

在我的 Twig 文件中:

````

 {% for features_topic in post.get_field('featured_topics') %}<div class="col-md-4 features-topics-widget"><h4>{{featured_topic.name}}</h4><ul>{% for topic_post %}<li>{{topic.title}}</li>{% 结束为 %}

{% 结束为 %}

上面的代码吐出了分类标题,这很好,但我一直在想如何显示属于它的帖子.任何帮助将不胜感激!!!

我写了这个查询(我不知道我在做什么).它拉出帖子,但只是重复.抱歉把问题放在 gitgub 上.:)

PHP

$topic_args = array('post_type' =>'邮政','posts_per_page' =>3、'tax_query' =>大批(大批('分类' =>'类别','字段' =>'term_id','条款' =>数组( 81, 92, 82, 1, 88, 86, 85))));$context['topic_post'] = Timber::get_posts($topic_args);

树枝

{% for features_topic in post.get_field('featured_topics') %}<div class="col-md-4 features-topics-widget"><h4>{{featured_topic.name}}</h4><ul>{% for cat in topic_post.posts('category') %}<li><a href="{{ cat.link }}">{{cat.name}}</a></li>{% 结束为 %}

{% 结束为 %}

解决方案

这里的功能将...

  • 编辑器创建帖子并从静态首页上的 ACF(特色主题")字段中选择 3 个类别
  • 在首页上,用户将看到三个选定的类别
  • 在每个类别下,用户会看到与该类别相关的三个其他帖子(因此总共有 9 个帖子)

home.php

$featured_topic_ids = get_field('featured_topics');//您可能需要根据您的 ACF 设置进行调整.//基本上,您正在尝试获取一组类别 ID,//然后将结果数组传递给 Timber::get_terms();$context['featured_topics'] = Timber::get_terms($featured_topic_ids);Timber::render('home.twig', $context);

home.twig

{% for ft in features_topics %}<h4>{{ ft.name }}</h4>{% for catpost in ft.posts(3) %}<li><a href="{{ catpost.link }}">{{ catpost.title }}</a></li>{% 结束为 %}

total noob with Timber and Twig. Im using Advanced Custom Fields with Timber and Twig. In the custom fields I have a field that allows the user to check 3 of any category through a taxonomy field type with the return value of term object.

What I am trying to do is show 3 of any category the user selected and 3 of the associated posts from that category underneath. I have like 14 categories overall.

Would look like this

In my PHP file I have

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

In my Twig file:

````

 {% for featured_topic in post.get_field('featured_topics')  %}
    <div class="col-md-4 featured-topics-widget">
        <h4>{{featured_topic.name}}</h4>

             <ul>
            {% for topic in topic_post %}
                    <li>{{topic.title}}</li>
            {% endfor %}    
            </ul>

    </div>

{% endfor %}

The code above spits out the category title which is fine but Im stuck figuring out how to show the posts that belong to it. any help would be appreciated!!!

Edit: I wrote this query (I dont know what Im doing ). It pulls the posts but just duplicates. Sorry putting the question on gitgub. :)

PHP

$topic_args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field'    => 'term_id',
        'terms'    => array( 81, 92, 82, 1, 88, 86, 85)
        )
    )
);

$context['topic_post'] = Timber::get_posts($topic_args);

TWIG

{% for featured_topic in post.get_field('featured_topics')  %}
    <div class="col-md-4 featured-topics-widget">
        <h4>{{featured_topic.name}}</h4>

            <ul>
            {% for cat in topic_post.posts('category') %}
                <li><a href="{{ cat.link }}">{{cat.name}}</a></li>
                {% endfor %}    
            </ul>   

    </div>

{% endfor %}

解决方案

So here is functionality that will...

  • Editor creates post and selects 3 categories from an ACF ("featured topics") field on a static front page
  • On the front page, a user will see three selected categories
  • Under each category the user sees three OTHER posts associated with that category (so 9 posts total)

home.php

$featured_topic_ids = get_field('featured_topics'); 
// you may need to adjust this based on your ACF setup. 
// Basically you're trying to get to an array of category IDs,
// then pass the resulting array to Timber::get_terms();
$context['featured_topics'] = Timber::get_terms($featured_topic_ids);
Timber::render('home.twig', $context);

home.twig

{% for ft in featured_topics %}
<h4>{{ ft.name }}</h4>

    {% for catpost in ft.posts(3) %}
         <li><a href="{{ catpost.link }}">{{ catpost.title }}</a></li>
    {% endfor %}

这篇关于Timber Twig Show 与类别相关的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆