I wonder if you mean this kind of a loop wrapper:
wpse_257857_loop(
$args = [ 'post_type' => 'page', 'posts_per_page' => 2 ],
$before="template-parts/wpse-before",
$loop = 'template-parts/wpse-loop',
$after="template-parts/wpse-after"
);
where we define it in the functions.php
file, in the current theme directory:
/**
* Simple loop wrapper
*/
function wpse_257857_loop( $args, $before, $loop, $after )
{
$defaults = [
'posts_per_page' => 10,
'ignore_sticky_posts' => true,
];
$args = wp_parse_args( $args, $defaults );
$query = new WP_Query( $args );
if( $query->have_posts() )
{
get_template_part( $before );
while( $query->have_posts() )
{
$query->the_post();
get_template_part( $loop );
}
get_template_part( $after );
wp_reset_postdata();
}
}
Note that we don’t need wp_reset_query()
, instead we use wp_reset_postdata()
.
We have to define the template parts, in the current theme directory, that we load with get_template_part()
.
Here’s an example:
-
/template-parts/wpse-before.php contains:
<div class="wpse-loop"> <ul>
-
/template-parts/wpse-after.php contains:
</ul> </div>
-
/template-parts/wpse-loop.php contains:
<li> <a href="https://wordpress.stackexchange.com/questions/257857/<?php the_permalink() ?>" > <?php the_title(); ?> </a> </li>
Hope you can adjust it to your needs!