Site logo

[Resolved] ACF Relationship fields in Elements Content Template

Home Forums Support [Resolved] ACF Relationship fields in Elements Content Template

Home Forums Support ACF Relationship fields in Elements Content Template

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2180644
    Traci

    I have custom post types for SIRES, DAMS, LITTERS.

    I have an Element Block -> content template I am building to display all litters on the litter archive.

    In each Litter I have a relationship with one Sire and one Dam. On the litter listing page, I would like the name (post title) and image (featured image) displayed for each Sire and Dam associated with the litter. Currently, I have selected Dynamic test type: Post Meta, and inserted father in the Meta Field Type (father is the name of the relationship field in the Litter custom field group), but all that shows on the front end is “Array”.

    Am I able to pull in relationship values from ACF into GeneratePress Elements? If so, can you help me with what value I need to put into the Meta Field Type to get the actual information from the related post type to display?

    Thanks in advance.

    #2180710
    Fernando
    Customer Support

    Hi Traci,

    You can retrieve values from ACFs through Dynamic Headlines, however there are limitations. One of them would be arrays.

    To address this, One approach is to create a Portable Hook through PHP code:

    function your_shortcode($atts, $content = null) {
          ob_start();
          do_action('hook_name');
          return ob_get_clean();
    }
    add_shortcode('portable_hook', 'your_shortcode');

    Now, we can use this portable_hook in our Content Template as such: https://share.getcloudapp.com/jkuvm5l6

    Then, we can retrieve the post meta manually through a Hook Element hooked to hook_name which is our portable hook: https://share.getcloudapp.com/kpuJ4g4D

    An example code would be:

    <?php 
    $relationships = get_post_meta(get_the_id(),'myrelationship',true);
    foreach($relationships as $relationship){
    	echo '<p class=relationships>' . get_the_title($relationship) . '</p>';
    }
    	
    ?>

    This code would retrieve all relationships and place them in a p tag. With this code hooked to our portable_hook, the specific relationships of your CPT should now appear.

    Kindly modify this code to your preference.

    Hope this helps! 🙂

    #2186963
    Traci

    Thanks I used a variation of this to work.

    #2186972
    Fernando
    Customer Support

    You’re welcome Traci! Glad to be of assistance! 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.