Customize Relationships Shortcode?
Support › MB Relationships › Customize Relationships Shortcode?
- This topic has 7 replies, 2 voices, and was last updated 2 years, 3 months ago by
Olivier.
-
AuthorPosts
-
December 24, 2022 at 4:17 AM #40017
Olivier
ParticipantHi 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
December 24, 2022 at 4:26 AM #40018Olivier
ParticipantI tried adding things like
items="thumbnail"
anditems="custom_field_id"
to the shortcode, but it doesn't work and my page renders all white.December 25, 2022 at 11:10 PM #40028Peter
ModeratorHello,
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"]
December 27, 2022 at 10:34 AM #40039Olivier
ParticipantWell, 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
December 27, 2022 at 9:43 PM #40041Peter
ModeratorHello,
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();
December 28, 2022 at 5:06 AM #40048Olivier
ParticipantThank 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?
December 28, 2022 at 9:03 PM #40052Peter
ModeratorHello,
Yes, you can put the code in the template file. Let me know if you have any questions.
December 29, 2022 at 1:06 AM #40054Olivier
ParticipantThank 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 ?
-
AuthorPosts
- You must be logged in to reply to this topic.