Overview
In Part 1 we created a bidirectional relationship between a Custom Post Type – Videos and a Post and also between Videos and another Custom Post Type -Events. So eventually a post about a press conference can have video(s) or Events can have Video(s).
Let’s get started
Below an event or post that has a linked video, I want to show the video’s meta which is present in custom fields (created by ACF) and custom taxonomies (created using the CPT UI). In our example a video has the following meta:
This should appear under a linked post or an event as below. Note, we are using the Video URL above and creating an embedded video as well.
Understanding where to Hook
I am using a child theme on the awesome Genesis framework. In order to add content below single posts or events, I will be using the ‘genesis_entry_footer’ action hook. In case you don’t use Genesis you will need to use ‘the_content’ filter hook or something similar.
Add the code below to your Genesis child theme’s functions.php
/** * Show linked objects in the posts and events pages */ add_action( 'genesis_entry_footer', 'nds_linked_entry_footer', 9 ); function nds_linked_entry_footer() { if ( is_singular( array( 'post', 'event' ) ) ) { //change your CPT here if(get_field('link_video') == "Yes") { //use the acf conditional field to check if we want videos echo '<div class="associated-videos">'; $videos = get_field('linked_objects'); //get all linked videos foreach($videos as $video) { $video_post_id = $video->ID; $video_url = get_field( "video_url", $video_post_id ); $video_terms = get_the_terms($video_post_id, 'artistes'); $dvd_available = get_field('dvd_available', $video_post_id); $video_genres = get_the_term_list($video_post_id, 'genres', "" , ", " , "" ); $video_artistes = get_the_term_list($video_post_id, 'artistes', "" , ", " , "" ); $video_title = '<a href="'.get_permalink($video_post_id).'">'.$video->post_title.'</a>'.'<br/>' ; echo '<div class="linked-video-item alignmiddle">'; echo '<h4 class="linked-video-title">' . $video_title . '</h4>'; echo '<div class="linked-video-frame"> '. wp_oembed_get($video_url) . '</div>'; echo '<div class="linked-video-excerpt"> '. get_field("video_excerpt", $video_post_id) . '</div>'; echo '<p class="entry-meta">'; echo __('DVD Available: ', 'yourtextdomain') . $dvd_available . '<br />'; echo __('Artistes: ','yourtextdomain') . $video_artistes . __(' Genres: ','yourtextdomain') . $video_genres . '<br />'; echo '</p>'; echo '</div>'; } echo '</div>'; } } }
Understanding what’s happening
Using the ‘genesis_entry_footer’ we add content below our post. The ‘is_singular’ statement allows us to conditionally add the linked video below our custom post type. You will need to modify ‘posts’, ‘event’ to include your CPTs. Rest of the stuff is pretty straightforward. We use the ‘get_field’ to retrieve the custom fields such as video_url, video_excerpt, and ‘get_the_terms’ to retrieve the custom taxonomies genres and artistes.
Leave a Reply