Skip Navigation

[Resolved] Display view in header

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display a Types field in header.php using a custom query?

Solution:
Outside of the Loop you need to specify the post id which is the source of the Types field, like so:

$mydate = types_render_field( 'date-field', array( 'id' => '$custom_query->ID' ) );
This support ticket is created 6 years, 7 months ago. 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.

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+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by briceJ 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#917402

Hi,

I would like display my view in my header.php.
But my custom fields do not appear but my post_title appear.
My code :
<?php $filtered_posts = get_view_query_results( 110 );
foreach ( $filtered_posts as $filtered_post ) {
echo $filtered_post->post_title;
$mydate = types_render_field('date-de-l-evenement');
if ( empty($mydate) ) {
echo 'Empty';
}
else {
echo $mydate;
}

echo '<br/>';
}
?>
Thank you

Regards,

#917408

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

If you are adding this code to a PHP template such as single.php or page.php then the types_render_field function doesn't have the right context to know the post from which it should be outputting the field values.

You use a foreach loop to iterate over the results of the View (which is an array of post objects), but the types_render_field will use the global $post object as its context, not your foreach loop.

You need to explicitly tell Types which post the fields belong to which you want to output, something like this:

types_render_field( "my-singlelinetext", array( ) )
$mydate = types_render_field( 'date-de-l-evenement', array( 'id' => '$filtered_post->ID' ) );
#917440

Ok it's works thanks.
And how can i get the last element without putting the id manually?
I have 3 news and i want the last news which has been inserted.

Thanks.

#917447

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Is this the only thing you use the View for?

You could update the settings for the View so that it is ordered by post date DESC with a limit of 1, so that it only returns the most recently published post.

Then there will only be a single post object returned by get_view_query_results

#917473

Thanks for trick.
Have you got a lexicon for all field i could call, like url or excerpt?

#917475

for $filtered_post->post_title like title.

#917479

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

These are the standard post fields available on the post object: https://codex.wordpress.org/Function_Reference/$post

#917481

thanks.
everything is working