Timber Twig Show Posts Associated with Category

做~自己de王妃 提交于 2019-12-02 21:10:56

问题


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 %}

回答1:


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 %}



回答2:


You need to create a grouped array,

PHP

$context['categories'] = Timber::get_terms('category');
$context['posts_per_category'] = [];
foreach($context['categories'] as $category) $context=['posts_per_category'][$category->ID] = Timber::get_posts('cat='.$category->ID);

Twig

{% for cat in categories %}
    <li>
        <a href="{{cat.link}}">{{cat.name}}</a>
        <ul>
            {% for post in posts_per_category[cat.ID] %}
            <li>... Do sthing with post ...</li>
            {% endfor %}
        </ul>       
    </li>
{% endfor %}


来源:https://stackoverflow.com/questions/42570245/timber-twig-show-posts-associated-with-category

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!