如何基于ACF Repeater字段在人员档案页展示关联自定义文章类型
Hey there! Let's walk through exactly how to pull in and display those linked Policy, Publication, Media, and Events entries on your person's profile page. I'm assuming you're using Advanced Custom Fields (ACF) (since you mentioned the Repeater field) and WordPress as your CMS—here's the step-by-step solution:
1. Double-Check Your Field Setup
First, make sure each of your custom post types (Policy, Publication, Media, Events) has the ACF Repeater field linked_author with a sub-field paper_linked_author set to the Post Object type. For the Post Object settings, restrict the selection to your "Person" custom post type (or user role, if your people are WordPress users instead of custom post types). This ensures authors can only link relevant people when creating/editing these entries.
2. Add the Query & Display Code to Your Person Template
Open up your person profile template file (this might be single-person.php if you're using a custom post type for people, or a template part like template-parts/person-related-content.php if you're using a theme builder). Paste in this code, which will query each custom post type and pull in entries linked to the current person:
// Grab the ID of the current person being viewed $current_person_id = get_the_ID(); // List of custom post types we want to fetch and display $related_post_types = [ 'policy', 'publication', 'media', 'events' ]; // Loop through each post type to fetch related entries foreach ($related_post_types as $post_type) { // Set up our WP_Query arguments $related_args = [ 'post_type' => $post_type, 'posts_per_page' => -1, // Show all linked entries (change to a number for pagination) 'post_status' => 'publish', 'meta_query' => [ [ // ACF uses a wildcard % to target all rows in the repeater 'key' => 'linked_author_%_paper_linked_author', 'value' => $current_person_id, 'compare' => '=' ] ] ]; $related_posts = new WP_Query($related_args); // Only display the section if there are linked entries if ($related_posts->have_posts()) { // Get the friendly name of the post type (e.g., "Publications" instead of "publication") $post_type_label = get_post_type_object($post_type)->labels->name; ?> <div class="person-related-content person-<?php echo $post_type; ?>"> <h3><?php echo esc_html($post_type_label); ?></h3> <ul class="related-items-list"> <?php while ($related_posts->have_posts()) : $related_posts->the_post(); ?> <li> <a href="<?php the_permalink(); ?>" class="related-item-title"> <?php the_title(); ?> </a> <!-- Optional: Add custom fields like date, excerpt, or thumbnail here --> <?php if (get_the_excerpt()) : ?> <p class="related-item-excerpt"><?php echo wp_trim_words(get_the_excerpt(), 20); ?></p> <?php endif; ?> </li> <?php endwhile; ?> </ul> </div> <?php } // Reset post data to avoid messing up other queries on the page wp_reset_postdata(); }
What This Code Does:
- It targets every row in the
linked_authorrepeater field using the wildcard%to check if anypaper_linked_authorentry matches the current person's ID. - It dynamically pulls the friendly name of each custom post type (so you don't have to hardcode headings like "Publications").
- It only renders a section if there are actual linked entries (no empty sections cluttering the page).
- You can easily add extra details like excerpts, dates, or custom fields by uncommenting the optional code block.
3. Style the Sections (Optional)
Add some CSS to make the related content look clean and consistent. For example:
.person-related-content { margin: 2rem 0; padding-bottom: 2rem; border-bottom: 1px solid #eee; } .person-related-content:last-child { border-bottom: none; } .related-items-list { list-style: none; padding-left: 0; } .related-item-title { font-weight: 600; color: #2d3748; text-decoration: none; } .related-item-title:hover { text-decoration: underline; } .related-item-excerpt { color: #718096; margin-top: 0.5rem; font-size: 0.9rem; }
4. Quick Notes to Avoid Issues
- Field Names: Double-check that your ACF field keys (
linked_authorandpaper_linked_author) match exactly in the code—ACF is case-sensitive! - People as Users: If your "people" are WordPress users (not a custom post type), replace
$current_person_id = get_the_ID();with$current_person_id = get_the_author_meta('ID');(assuming you're using an author archive page for profiles). - Pagination: If you have lots of linked entries, change
posts_per_pagefrom-1to a number (like 10) and add WordPress pagination code after the loop.
内容的提问来源于stack exchange,提问作者Rakesh Ojha

