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;
}
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:
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.
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