Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twig 木枝显示与类别相关的帖子_Twig_Timber - Fatal编程技术网

Twig 木枝显示与类别相关的帖子

Twig 木枝显示与类别相关的帖子,twig,timber,Twig,Timber,带木材和细枝的总noob。我使用先进的木材和树枝自定义领域。在自定义字段中,我有一个字段,允许用户通过返回值为term object的分类字段类型检查任何类别的3 我试图做的是显示用户选择的任何类别中的3个,以及下面该类别中的3个相关帖子。我总共有14个类别 看起来像这样 在我的PHP文件中,我有 $context['categories'] = Timber::get_terms('category'); 在我的小树枝文件中: ```` 小枝 {post.get_字段('featured_

带木材和细枝的总noob。我使用先进的木材和树枝自定义领域。在自定义字段中,我有一个字段,允许用户通过返回值为term object的分类字段类型检查任何类别的3

我试图做的是显示用户选择的任何类别中的3个,以及下面该类别中的3个相关帖子。我总共有14个类别

看起来像这样

在我的PHP文件中,我有

$context['categories'] = Timber::get_terms('category');
在我的小树枝文件中:

````

小枝

{post.get_字段('featured_topics')中的featured_主题的百分比%}
{{featured_topic.name}
    {主题_post.posts('category')中cat的百分比%}
  • {%endfor%}
{%endfor%}
您需要创建一个
分组的
数组

PHP

小枝

{%类别中的cat%}
    • {每个类别[类别ID]%的帖子中的帖子占%
    • 。。。用邮局做某事
    • {%endfor%}
  • {%endfor%}
    因此,这里的功能将

    • 编辑器创建文章并从静态首页上的ACF(“特色主题”)字段中选择3个类别
    • 在首页上,用户将看到三个选定类别
    • 在每个类别下,用户会看到与该类别相关的其他三篇文章(总共9篇)
    home.php 小枝
    {%ft在特色主题%}
    {{ft.name}
    {ft.POST中catpost的百分比(3)%}
    
  • {%endfor%}
    我已将自定义字段设置为对象。由于某种原因,它仍然不起作用。邮局。通过添加for语句,我还没有添加DarkBee的分组数组。仅使用Timber$context['categories']=Timber::get_terms('category');你好,杰瑞德。此功能将出现在静态首页上。基本上,管理员会进入主页编辑屏幕,然后看到所有类别的选择列表(我们称之为主题)。他们可以选择三个类别。一旦他们保存并转到前端主页。将分别显示3个主题标题和3个帖子。所以是的。3个类别和9个员额。非常感谢您的帮助:)哎呀,我是说3个类别标题,以及与特定类别相关的帖子标题。Jared!!!非常感谢你。这很有魅力。我只需要将自定义字段更改为term_id。它会抓住帖子!!
     {% 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 %}
    
    $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);
    
    {% 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 %}
    
    $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);
    
    {% 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 %}
    
    $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);
    
    {% 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 %}