WordPress plugin that helps you to automatically inject CPT posts into the main loop
WordPress plugin that helps you to automatically inject CPT posts into the main loop.
Requires PHP 5.3.
##Description##
Posts can be injected using posts_syringe()
function.
It accepts 3 arguments:
$cpt
The CPT(s) to inject in the main loop$args
Arguments that control how posts are injected. Accepted values:
$query_args
Additional query args to query posts to injects. 'post_type'
and 'posts_per_page'
are set automatically, use this argument for additional query args, e.g. 'tax_query'
and so on##Quick Example##
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_main_query() && ( $query->is_home() || $query->is_archive() ) ) {
posts_syringe(
'sponsor',
array( 'before_each_inject' => 3, 'per_inject' => 1 ),
array( 'meta_key' => 'featured', 'meta_value' => '1' )
);
}
} );
Code above injects 1 'sponsor' post every 3 posts in the main loop, in home page or archive pages. Only sponsor posts having meta key 'featured' equal to '1' will be queried and injected.
Note that is possible to use more than one posts_syringe()
calls with different arguments at same time.
Posts are injected in the main loop and they will be trated as posts being part of it.
However, inside templates is possible to distinguish from regular main loop posts from ones that were been injected.
That is possible thanks to the is_syringe_injected()
template tag.
Example:
while ( have_posts() ) : the_post(); ?>
if ( is_syringe_injected() ) {
get_template_part( 'sponsor-entry' );
} else {
get_template_part( 'content', get_post_format() );
}
endwhile;
##License##
GPLv2+