Skip Navigation

[Resolved] How to call a View or Content Template from a PHP template file?

This support ticket is created 5 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Author
Posts
#1254497

I've been searching your documentation for days, trying different approaches to my problem. I have a site that uses a custom home.php template to display only the post from the current day. It's a membership site that shares a free story daily, but once the day is over the post goes into the protected archive for members only. We want the public to freely see the story and leave comments while the story is displayed on the home page, but only members to access it once the day is over and a new post arrives.

This used to work using the Molongui Authorship plugin, which used custom post types for the authors. But due to conflicts with the Membership 2 Pro plugin, I am trying to replicate this functionality with Toolset. Each story has a different author, and authors are NOT always registered users in the WordPress installation. So I've set up a custom post Type for the authors, and created a one-to-many relationship where an author can be the parent to many posts (stories). This seems to be working really well for single post display.

I even created a Layout for a home page that displays the most recent 1 post and shows the associated parent-author at the top by the title, and then a full Content Template with the author's picture and bio at the end of the story (hidden link).

But I'm having difficulty making the last part of the functionality: I can't seem to get the comments to display for the current post in the loop. Using the Layout interface, I can only insert the comments from the current Page (the page hosting the loop that displays the current post).

So then I thought I'd use a home.php template file like I have in the past, and simply insert the appropriate Layout or View or Content Template shortcodes where they belong. But despite searching the documentation for days, I have not yet found the simple explanation of how to call those items from a PHP file. It seems like it should be super simple, but without the proper syntax I have no idea what to try.

I did see the example files in the Types plugin folders, but they aren't calling an entire Layout or Content Template or View, just individual fields (at least that's all I noticed). Any tips or help on this would be much appreciated!

#1254509

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Teddi,

Thank you for getting in touch.

For the Layout you wouldn't be able to do this, however for the view and content template you can use the functions below.
https://toolset.com/documentation/programmer-reference/views-api/#render_view

Please let me know if this helps.

Thanks,
Shane

#1254605

Thank you! That looks like what I need, but I'm having trouble making it work. The example in the documentation is:

echo render_view_template( 80, $mypost );

I put in my home.php the following variations (inside the while/endwhile loop that displays the current post):

<?php echo render_view_template( 1914, $mypost ); ?>
<?php echo render_view_template( 1914 ); ?>
<?php echo render_view_template( 1914, 1244 ); ?>

1914 is the ID of the Content Template (found by hovering my mouse over the link to edit it - hidden link ).

I don't know how to tell it to use the current post in the loop (is $mypost something native to WordPress, or is the example just illustrating the case where you have a variable for the current post called $mypost?). So I tried telling it to use the content template on a specific post (1244) just to see what it would output -- but nothing appeared. What am I missing here? (Please have mercy, I am *not* well-versed in PHP, but in the past I've been able to understand enough to adjust existing pages or use snippets of code supplied by others.)

#1255189

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Teddi,

Is there a specific post that you want to render with the template because the $mypost should be a post object.

If it is a single post that you want to retrieve I should be able to assist you in getting the post.

Please let me know.
Thanks,
Shane

#1255393

It needs to display something associated with the current post in the loop, not a specific hard-coded post ID. I'm trying to display the author box associated with the current story, and the Content Template displays data from the author custom post that is the relational parent of the current post.

Because this is happening on a Page (home.php) that is displaying the content of a post via custom query, it's important that it doesn't try to display the Content Template for the current page but instead the one for the current post in the loop.

Hope I explained that clearly. {:-)

#1255429

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Teddi,

I think i'm following, why not use a view to display the author box associated with the current story. A content template will only load a single post.

But the view if you have more than one post in the relationship will display all of them and all it really needs is the ID of the parent page.

So if you are already retrieving the parent post on this page then you can simply filter the child view by the parent ID.

Let me know if i'm following correctly.

Thanks,
Shane

#1255503

The current post that is being displayed is the child post (a story written by an author). My goal is to display below the story a formatted "author box" that contains fields from the parent custom post (the parent of the story, which is the author of the story).

Would a View work better than a Content Template for displaying the parent of the current post? There will not be multiple parent posts associated with the current post (so if Views are best for displaying lists, I don't need that).

Here's my home.php template code (minus some of the theme's layout stuff):

<?php // THIS PART gets the current day's post only
date_default_timezone_set('America/Los_Angeles');
$today = getdate();
$args_today = array(
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
),
'posts_per_page' => '1'
);
$query_today = new WP_Query( $args_today );

// NEXT PART is for assigning the placeholder page if there isn't a post scheduled for today
$args_pageid = array(
'page_id' => '287',
);
$query_pageid = new WP_Query( $args_pageid );
?>

<?php // NEXT we create our custom query
if ( $query_today->have_posts() ) {
$custom_query = $query_today;
} else {
$custom_query = $query_pageid;
} ?>

<?php // HERE WE DISPLAY the content of today's post, or the placeholder if one isn't scheduled for today
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('gridlove-box box-vm'); ?>>

<?php get_template_part('template-parts/home/content-'.$layout['content']); ?>

</article>

>>>>> THIS IS WHERE I TRIED TO RENDER THE VIEW TEMPLATE

<?php endwhile; ?>

I'm just looking at the syntax of the documentation page example and am not sure how to specify "the current post" when I don't know exactly which post ID it will be.

Their example: <?php echo render_view_template( 80 , $mypost ); ?>

Is an argument required there, or should it default to the current post if it's in a loop like this? Now that you can see my code, what variable (or PHP code) could I place there for the current post?

<?php the_ID(); ?> does return the correct story/child post ID, the current one in the loop. Is there a way to put _that_ as an argument inside the render shortcode?

#1257395

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Teddi,

So you will need to call the template here.


<?php // HERE WE DISPLAY the content of today's post, or the placeholder if one isn't scheduled for today
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
 echo render_view_template( 80 , $custom_query->the_post() ); 
<article id="post-<?php the_ID(); ?>" <?php post_class('gridlove-box box-vm'); ?>>

<?php get_template_part('template-parts/home/content-'.$layout['content']); ?>

</article>

I'm assuming $custom_query->the_post() is retrieving each post object.

Thanks,
Shane