Created
February 3, 2024 18:27
-
-
Save contemplate/167464f3a0de4a8dfad00a8e925d10bd to your computer and use it in GitHub Desktop.
ACF Relational Field Shortcode Expanded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Output an ACF post relationship field type using a shortcode: | |
* [acf_relationship_field field="field_name" show="all"] | |
* You can also pass `post_id` as an attribute | |
* | |
* Show parameter accepts any comma seperated combination of: all, title, title_linked, date, author, post_excerpt, post_content | |
* | |
* Works with Post Object and Relationship fields, when the return | |
* format is both post object and post ID | |
* | |
* This code assumes you are copying this into functions.php of | |
* your child theme. | |
**/ | |
// Setup the shortcode | |
add_shortcode( 'acf_relationship_field', 'shortcode_get_relationship_field' ); | |
function shortcode_get_relationship_field( $atts ) { | |
ob_start(); | |
global $post; | |
$defaults = array( | |
'post_id' => $post->ID, // Default to the current Post ID. | |
'field' => '', | |
'show' => 'all', // Default to showing all elements | |
); | |
$args = wp_parse_args( $atts, $defaults ); | |
$related_posts = get_field( $args['field'], $args['post_id'] ); | |
// Check if we have a single post (post object, post ID) or an | |
// array of posts then convert to array. | |
if( is_object( $related_posts ) ) { | |
$related_posts = array( $related_posts ); | |
} else if ( is_scalar( $related_posts ) ) { | |
$related_posts = array( $related_posts ); | |
} | |
if ( $related_posts ) { | |
foreach( $related_posts as $related_post ) { | |
if ( is_scalar( $related_post ) ) { | |
$related_post = get_post( $related_post ); | |
} | |
?> | |
<div class="related-post"> | |
<?php | |
if (strpos($args['show'], 'title') !== false) { | |
if (strpos($args['show'], 'linked') !== false) { | |
echo '<h2 class="related-post-title"><a href="' . esc_url( get_permalink( $related_post->ID ) ) . '">' . esc_html( get_the_title( $related_post->ID ) ) . '</a></h2>'; | |
} else { | |
echo '<h2 class="related-post-title-unlinked">' . esc_html( get_the_title( $related_post->ID ) ) . '</h2>'; | |
} | |
} | |
if (strpos($args['show'], 'date') !== false) { | |
echo '<p class="related-post-date">' . esc_html( get_the_date( '', $related_post->ID ) ) . '</p>'; | |
} | |
if (strpos($args['show'], 'author') !== false) { | |
echo '<p class="related-post-author">' . esc_html( get_the_author_meta( 'display_name', $related_post->post_author ) ) . '</p>'; | |
} | |
if (strpos($args['show'], 'post_excerpt') !== false) { | |
echo '<p class="related-post-excerpt">' . esc_html( get_the_excerpt( $related_post->ID ) ) . '</p>'; | |
} | |
if (strpos($args['show'], 'post_content') !== false) { | |
echo '<div class="related-post-content">' . apply_filters( 'the_content', $related_post->post_content ) . '</div>'; | |
} | |
?> | |
</div> | |
<?php } ?> | |
<?php | |
} | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to this gist for getting me started.
Example usage:
[acf_relationship_field field="your_field_name" show="title_linked,date,author,post_excerpt"]
Accepted show parameters:
all -> shows post title (linked), post date, post author, post excerpt
title -> shows the post title not linked
title_linked -> shows the post title linked to the post
date -> shows the post date
author -> shows the post author (not linked)
post_excerpt -> shows the post excerpt
post_content -> shows the full post content