Skip Navigation

[Gelöst] Show Parent Field in the Child Single Post

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
How to show fields from parent posts when creating a PHP template for child posts?

Solution:
When you create templates with Views or Layouts you can set the context to the parent post when outputting fields with the id shortcode attribute.

You can employ a similar technique when directly editing PHP templates with the types_render_field function, passing the ID attribute as an array:

types_render_field( 'contact-info', array( 'id' => '$twi-vendor' ) );

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/displaying-parent-posts/

This support ticket is created vor 6 Jahre, 3 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 3 Antworten, has 2 Stimmen.

Last updated by Nigel vor 6 Jahre, 3 Monate.

Assisted by: Nigel.

Author
Artikel
#606615
types-1.jpg

Hello,

i have CPT vendor as a parent post

i have CPT package as child post

one vendor can have more than 1 package

i already connect the packages as child to its parent vendor

and my problem now i want query parent field in child post single post

any idea about this ?

i have try using this

[code]
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>

<div align="center">[wpv-post-featured-image id='$twi-vendor']</div>
<div style="padding: 10px; border: 1px solid #ccc; margin-top: 20px;">
<div align="center">Contact Info</div>
<div style="margin-top:15px;">[types field='contact-info' id='$twi-vendor'][/types]</div>
</div>

</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]
[/code]

but that query only showing the latest parent post, and that is not the parent of the child
need some guide here

the link you see here : hidden link

i attached screenshot about this,

#606679

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

Hi Victor

You are using id='$twi-vendor' to reference the parent post both in the Types contact field and for the post featured image before that.

I take it that the types field is referencing the wrong parent post, but what about the featured image? Is that correct or not?

#606682

hello Nigel

from the picture, image on the left side is picture image from parent post vendor and that is wrong parent

and the image on the right side is the feature image of package child and this correct image but this is not parent

just for little noted, im using toolset view to loop package CPT and then i place do shortcode to single-twi-package.php

and for the left of picture (the parent) i'm using toolset view loop and place the shortcode to sidebar, this sidebar will load only on package CPT

still need guide about this how to output correct parent field in its child post

thanks

#606726

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

I don't understand why you are using Views in your PHP single post templates.

We have a guide to displaying fields from post parents when editing PHP templates here: https://toolset.com/documentation/customizing-sites-using-php/displaying-parent-posts/

It's not clear from that page, but you can specify the same id='$twi-vendor' parameter directly in the function types_render_field, equivalent to the types shortcode, but used directly in the PHP template.

So you can add this to your single-twi-package.php template and it should output the field of the parent:

echo types_render_field( 'contact-info', array( 'id' => '$twi-vendor' ) );

For standard fields such as the featured image field you would need to use the technique shown in the linked documentation page to get the id of the parent post using the function wpcf_pr_post_get_belongs();

Try updating your template according to the guide and let me know how you get on.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.