Download the PHP package freshflesh/wp-render-template-part without Composer
On this page you can find all versions of the php package freshflesh/wp-render-template-part. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download freshflesh/wp-render-template-part
More information about freshflesh/wp-render-template-part
Files in freshflesh/wp-render-template-part
Package wp-render-template-part
Short Description An alternative to the native WP function `get_template_part` that can pass arguments to the local scope
License MIT
Homepage https://github.com/thomascharbit/wp-render-template-part
Informations about the package wp-render-template-part
render_template_part()
An alternative to the native WordPress function get_template_part()
that can pass arguments to the local scope.
Installation
- Using composer :
composer require freshflesh/wp-render-template-part
- Or manually download and include the
src/wp-render-template-part.php
file
Parameters
- $slug: (string) (Required) The slug name for the generic template.
- $name: (string) (Optional) The name of the specialised template. Default value: null
- $args: (array) (Optional) An associative array containing arguments to pass to the template. Default empty array.
- $echo: (boolean) (Optional) Wether to echo or return the rendered template. Default is true.
Usage
To pass arguments to a template partial, use the render_template_part()
function.
Simple example
Parent template:
</h2>
<p></p>
</div>
Returning value example
Useful when returning templates through AJAX for instance:
<?php
// get rendered template
$html = render_template_part( 'partials/sidebar', 'article', array(
'post_id' => $post->ID
) );
// send in json response
wp_send_json_success( array( $html );
Features
WP_Query autoloading
Passing a WP_Query instance as a query
argument will automatically set it as the current loop.
WP_Post autoloading
Passing a WP_Post instance as a post_object
argument will automatically call setup_post_data()
so you can start using the_title()
, the_content()
.
Note:
query
andpost_object
are therefore reserved argument names
Extendable
2 hooks are provided to extend its behavior:
Action:
do_action( "render_template_part_{$slug}", $slug, $name, $args, $echo );
Filter:
$args = apply_filters( "render_template_part_{$slug}_args", $args, $slug, $name, $echo );