Support Forum
Support › MB Relationships › Customize Relationships Shortcode?
Hi team,
I'm using Relationships with shortcodes (the only way I can use it), and I'm currently able to display the title of the related post with:
[mb_relationships id="licensee-events" mode="inline"]
Is it possible to display other fields from the related post, e.g. featured image, custom field values, etc? I looked at the documentation but there is very little explanation for how to use the shortcode, and no examples. However, the doc mentions the items
parameters, albeit with no direction on how to use it, but I'm wondering if I can use it for that purpose.
Could you give a few examples of how to use the shortcode for other post fields than the title?
Thank you
I tried adding things like items="thumbnail"
and items="custom_field_id"
to the shortcode, but it doesn't work and my page renders all white.
Hello,
It is not possible to display featured images, custom fields value ... of the related posts with the shortcode. The attribute items
is used to pass the list of post IDs that you want to display the related posts of those posts by using the shortcode. For example:
[mb_relationships id="posts_to_pages" items="12,34,56" direction="from" mode="ul"]
Well, great 😕
Is it possible to see a full example of code that one might use to fetch that data from a post type once the relationship is established via the UI? E.g an example of fetching title, featured image, custom field. The documentation only shows very partial code and I'm willing to figure things out, but I can't even find enough examples to build from or even ask on forums about. So far all I've got is 'learn to code' and I would be happy to learn from an example. Thank you
Hello,
The example of code is added to the documentation, please check it here https://docs.metabox.io/extensions/mb-relationships/#posts
To get the featured image or other post data, you also need to have a basic knowledge of WordPress coding. For example https://developer.wordpress.org/reference/functions/the_post_thumbnail/
$connected = new WP_Query( [
'relationship' => [
'id' => 'posts_to_pages',
'from' => get_the_ID(), // You can pass object ID or full object
],
'nopaging' => true,
] );
while ( $connected->have_posts() ) : $connected->the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
the_post_thumbnail( 'thumbnail', array( 'class' => 'alignleft' ) );
endwhile;
wp_reset_postdata();
Thank you Peter, even just that is incredibly more useful than the documentation.
And to clarify, I would put this code in my template file, right?
Hello,
Yes, you can put the code in the template file. Let me know if you have any questions.
Thank you. And if I want to filter a Query Loop by some fields of a related post, would I use the code described in https://docs.metabox.io/extensions/mb-relationships/#post-archive ?