You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Timber Twig的single-project页面添加关联项目包含模块的技术问询

Hey there! Let’s break down the two most common technical issues folks hit when setting up related projects with ACF, Twig, and a custom PHP logic file—plus how to fix them:


This usually happens when the project data you’re querying in related.php isn’t being passed into the Twig template context properly. Here’s how to fix it:

First, make sure your query is fetching 3 relevant projects (and excludes the current project). For example, if you’re using project categories to match related content:

// related.php
function get_related_projects() {
    global $post;
    
    // Grab the category IDs of the current project
    $project_categories = wp_get_post_terms($post->ID, 'project_category', ['fields' => 'ids']);
    
    $query_args = [
        'post_type' => 'projects',
        'posts_per_page' => 3,
        'post__not_in' => [$post->ID], // Exclude the current project
        'tax_query' => [
            [
                'taxonomy' => 'project_category',
                'field' => 'id',
                'terms' => $project_categories,
            ],
        ],
    ];
    
    $related_query = new WP_Query($query_args);
    // Return the posts array for Twig to use
    return $related_query->have_posts() ? $related_query->posts : [];
}

Step 2: Pass the data to your Twig templates

In the PHP file that renders single-project.twig (usually single-project.php), inject the related projects into the template context before compiling:

// single-project.php
$context = Timber::context();
$context['post'] = new Timber\Post();
// Add related projects to the context
$context['related_projects'] = get_related_projects();

// Render the main single project template
Timber::render('single-project.twig', $context);

Now you can loop through the related_projects variable in your included template:

{# inc-related.twig #}
{% if related_projects is not empty %}
    <section class="related-projects">
        <h3>Related Projects</h3>
        <div class="projects-grid">
            {% for project in related_projects %}
                <article class="project-card">
                    <h4>{{ project.post_title }}</h4>
                    {# Display an ACF field like project cover image #}
                    {% set cover_image = TimberImage(project.meta('project_cover')) %}
                    <img src="{{ cover_image.src }}" alt="{{ project.post_title }}">
                    <a href="{{ project.link }}">View Project</a>
                </article>
            {% endfor %}
        </div>
    </section>
{% endif %}

If you’re getting too many projects, duplicates, or irrelevant content, your query logic needs tweaking. Here’s how to adjust it:

Scenario A: Using an ACF Post Object field for manual related projects

If you have an ACF field (e.g., related_projects) where editors manually select related projects, update your related.php query to prioritize that selection first:

// related.php
function get_related_projects() {
    global $post;
    
    // Check for manually selected related projects via ACF
    $manual_related_ids = get_field('related_projects', $post->ID);
    
    if (!empty($manual_related_ids)) {
        // Exclude the current project if it's accidentally selected
        $cleaned_ids = array_filter($manual_related_ids, function($id) use ($post) {
            return $id !== $post->ID;
        });
        
        $query_args = [
            'post_type' => 'projects',
            'posts_per_page' => 3,
            'post__in' => $cleaned_ids,
            'orderby' => 'post__in', // Keep the order editors selected
        ];
    } else {
        // Fallback to category-based matching if no manual selection
        $project_categories = wp_get_post_terms($post->ID, 'project_category', ['fields' => 'ids']);
        $query_args = [
            'post_type' => 'projects',
            'posts_per_page' => 3,
            'post__not_in' => [$post->ID],
            'tax_query' => [
                [
                    'taxonomy' => 'project_category',
                    'field' => 'id',
                    'terms' => $project_categories,
                ],
            ],
        ];
    }
    
    $related_query = new WP_Query($query_args);
    return $related_query->have_posts() ? $related_query->posts : [];
}

Quick Debug Tip

If you’re still seeing odd results, add var_dump($related_query->posts); to related.php to check what data is actually being pulled. This helps spot if your query is returning empty, duplicates, or the wrong posts.


内容的提问来源于stack exchange,提问作者filnug

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:50:51