Skip Navigation

[Resolved] Render Content Template with PHP

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

Problem:
How to show a Content Template with PHP?

Solution:
To call a Content Template you can use the API here:
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

For example the code below will show the post $my_post with the CT ID 80

echo render_view_template( 80, $mypost );
This support ticket is created 6 years, 2 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Peter 6 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#1113447

Tell us what you are trying to do?
I'm trying to display a page's content in a popup. I'd like to show the whole content with the content template also. I'm using the "new WP_Query" method to create a loop, where I select the page_id argument to show only the desired page. The get_the_content function is only returning the post body, without the content template.

Is there any documentation that you are following?
I have not found any documentation for this job.

Is there a similar example that we can see?
There is no example.

What is the link to your site?
Here is the page, where I'm showing markers on an image:
hidden link

If you click on any marker, a popup will show up with this page's content:
hidden link

As you can see, the page has other content also, which is displayed with a content template.

Please let me know, how can I display the entire content of a page (without header and footer and other parts of the page. Only the content with the content template.

Thank you!

#1113561

To call a Content Template you can use the API here:
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

For example the code below will show the post $my_post with the CT ID 80

echo render_view_template( 80, $mypost );
#1113573

Thank you Beda for your help! I just found the same solution, which is working for me.

I have a problem with it. There are php notices about this function:
Notice: Trying to get property 'post_author' of non-object in /wp-content/plugins/wp-views/embedded/inc/wpv-api.php on line 262

The problem here is, that in that code, you want to use the $post as an object, but it is not. It is only an id of a user. So you need to fix this to get the user id with get_post_field function. Just change the 261-262. lines in the wpv-api.php file to this:

$authorid = get_post_field( 'post_author', $post_in );
$authordata = new WP_User( $authorid );
#1113580

I am not entirely sure what you mean.

Can you share the code you use to throw the notice?
If you echo a render_view_template( 80, $mypost ); and $mypost is really a Post object, it works just fine, I tried it.
Of course, if you pass anything else in $mypost, the ID of the post as an example, then you get an error:

Notice: Trying to get property 'post_author' of non-object in /Applications/MAMP/htdocs/stable/wp-content/plugins/wp-views/embedded/inc/wpv-api.php on line 262
Call Stack

But, that is not a BUG, as you need to pass the Post Object, to the function, not the Post ID or any else.

Example (Working):

global $post;
			echo "start";
			echo render_view_template( 12, $post );
			echo "end";

Do I misunderstand you?

#1113634

Yes, you are right, I was passing the ID, instead of the object. Thank you!