I am using the function 'render_view_template()' within the functions.php file within my theme. I am able to call the content template just fine except that it does not render any of the dynamic data from a particular post.
This is a continuation of this post:
https://toolset.com/forums/topic/displaying-a-content-template-programmatically-does-not-render-contained-views/
Using 'render_view_template()' inside a single-custom_template_slug.php file will render the dynamic data contained in shortcode fine but ideally I really want to use this functionality within the file functions.php. Once again, here's the output I receive when using this function inside functions.php
[wpv-post-title] // This should render the actual title of the post instead of displaying this shortcode
/* Content there */
Any help is greatly appreciated.
Hi, can you show me exactly how you're using this code in functions.php? Is this part of a shortcode, or an action hook, or what? I'd like to review all the relevant code.
Hi Christian,
So essentially what I'm trying to do is create a route using this function I have called WP_Route. When a particular route is reached, it should render a function that will call the content_template assigned to that custom post type.
Here is my code:
// The route
WP_Route::get('/items/{group}/{subgroup}/{title}/{id}', 'render_item');
function render_item()
{
get_header();
global $post;
$getgroup_subgroup = explode("/", $_SERVER[REQUEST_URI]);
$query = new WP_Query( array(
'post_type' => 'item',
'posts_per_page' => 1,
'order' => 'ASC',
'orderby' => 'title',
// Use 'meta_key' and 'meta_value' to query on Custom fields
'meta_key' => 'wpcf-item-id',
'meta_value' => $getgroup_subgroup[5],
));
if ( $query->have_posts() ) {
while ( $query->have_posts() ) :
$query_data = $query->the_post();
$filtered_posts = render_view_template(12345, $query_data);
echo $filtered_posts;
endwhile;
}
}
Please review the syntax for the render_view_template function here: https://toolset.com/documentation/programmer-reference/views-api/#render_view_template
It looks like you're trying to render a template with ID 12345 and for the 2nd parameter you're passing in each post object in the loop. The only thing I see obvious here is the 12345 ID. That seems like dummy data that should be replaced with an actual template ID. If that's still not working, try logging the contents of $query_data just before you pass it into the render_view_template function:
$query_data = $query->the_post();
error_log(print_r( $query_data, true ) );
$filtered_posts = render_view_template(12345, $query_data);
Or just var_dump it if you're not using the logs. Let me know what you get.
Hey Christian,
So I figured out what exactly was happening with why the Content Template wasn't rendering the dynamic content programmatically.
So I was using a custom route function within WordPress to dynamically generate routes that you would see within an MVC web app. As I learned, this type of MVC routing in WordPress is not native to this CMS, so you will need a plugin to make this possible. This route is defined within my functions.php file, which essentially calls a custom function that you define within that file. Within that custom function, that's where I was initially calling the function 'render_view_template()' and passing along parameters to dynamically render the content template in my code.
I've learned that you can only really use 'render_view_template()' within a 'single' (template) file in WordPress if you want to call a content template and/or view explicitly and have it render dynamic content.
The workaround for this scenario is to do the following:
1) If you want to use a route within WordPress, use this function hidden link Take the code from the file WP_Route.php and create a WordPress plugin from it.
2) Define a custom route within your functions.php file and have it call a custom function
3) Within the custom the function, create an array of arguments ($args) that will fetch a desired custom post
4) Instead of using render_view_template(), use 'query_posts($args)'
5) When 'query_posts($args)' is called, it will essentially call the 'single' file that is associated with your custom post. In the 'single' file, this is where you call the functions 'the_post()' and 'the_content()' to render out the content for your custom post. This will render out the Content Template with your dynamic data properly displayed.
I hope this information helps anyone else who comes across this issue.
Thanks for your help Christian!