I couldn’t figure out a WordPress way to do this and looking at the WP source, it looks like this probably will never really be possible. However, I was able to get a solution using vanilla PHP by redirecting standard output into a buffer and loading the buffer into a variable.
$listings = new WP_Query(...);
ob_start();
while ( have_posts() ) {
get_template_part('content', $post->post_type);
}
$markup = ob_get_contents();
ob_end_clean();
return $markup
Related Posts:
- if ( is_home() && ! is_front_page() )
- Second Loop Showing Only One Post on Single Post Page
- Jquery Slider for profile template
- Why do themes rely on “The Loop”?
- Is including the loop necessary for page.php? [duplicate]
- What is best way passing variables to theme templates and using them different places like widgets?
- include(locate_template) messing up the loop
- Page template not displaying
- Loop through pages with specific template
- How to get_template_part using AJAX?
- wp alchemy multiple image uploader output images to template
- code suddenly appearing from my tempate second loop
- Get search.php results in header.php?
- WordPress Loop inside Loop?
- Template structure
- How to show “teaser” posts on blog / archive pages, otherwise full posts
- Order archive results by post id in custom taxonomy template
- Default ‘post’ post type archive slug?
- How do I make a variable available inside partials?
- Update status of all posts in a category
- 404 Template customization | Want 10 recent Post on the 404.php error page apart from the error Notice
- How do I put my frontpage featured image in a page template for home.php?
- Call wp-blog-header.php header without performing a wp_query
- How to render the single post template with the post name?
- excerpt not showing up
- How to add a byline to all the pages?
- new custom theme – posts displaying on top of one another
- How do i create a list-posts-page?
- How to sort a loop after most viewed
- How to loop specific Category menu from archive page?
- How do I set the Stewart Search Template to return only published posts in the result
- Is it possible to set archive.php instead of index.php to display blog?
- Using is_page_template inside loop
- Trying to get property ‘post_content’ of non-object
- is it bad to copy the loop for a template?
- Is “the loop” a template tag?
- Deeply Nested Menu Loop with Twig using Timber [closed]
- WordPress with woocommerce custom query
- Infinite Loop after Genesis Theme Upgrade
- Alternative layout the loop
- Cant pull in featured Image
- Template included with plugin doesn’t return all posts for custom taxonomy
- Creating a archive for taxonomy terms, not the term results
- Default Template infinite loop of Title
- Secondary loop cuts off at 10 posts?
- Need a conditional to test if title of parent page matches title of child page
- get all page templates
- Load featured article once in a loop
- wordpress showing all posts instead of date range
- How to implement template file and the loop
- Same posts within a paginated page
- Meaning of “if ( is_home() && ! is_front_page() )” snippet?
- How can I create an entirely new, separate display of posts?
- Page template in two level deep folder
- How to place comments_template(); outside the loop?
- Loading template files from a subfolder in my theme?
- How do I prevent one of two multiple loops from repeating on a second page?
- Hook into the loop via a plugin, and output something after every X post?
- How are both HTTP and HTTPS versions displaying?
- How do you split multiple-column loop on category archive page?
- How to Loop within a Loop (Display Children and then Grandchildren)
- Can I apply a WP theme to a specific custom page template?
- How to loop through ALL pages?
- Does get_template_part pull data once in a loop?
- Custom Post Type Taxonomies -Posts not showing in Category or Tag pages
- Get titles of all posts with current tags except current post
- Creating a multi-taxonomy query, excluding the newest (read: highest ID) taxonomy term
- Show a different post every 3 days [duplicate]
- Using get_the_excerpt() Before The Loop
- What is the maximum number of posts a WordPress loop can work with?
- wp_query inside the_loop
- How to go about combining dropdowns / filter queries?
- Advanced Custom Field DatePicker [closed]
- WordPress Rewriting the DOM?
- How to display posts by vote count and if no value continue with latest posts with no votes?
- Sort results by groups of numbers and letters
- Basic multiple loops only displaying first loop
- Display post list with different styles
- Array ids post to function have_post
- Where to find the code used to render a page that has a shortcode and a template defined?
- Give each posts in a loop a number in sequence
- custom post on homepage
- I have broke my pagination, same posts on all pages (index.php)
- Show terms in archive page
- Multiple loops for plugin
- Sort posts according to a numeric value entered with ACF
- wp_get_attachment_url not fetching URL?
- How to get all multi-select user meta values and add them to an array?
- How to resolve a reload loop issue in the frontend when logged in to WordPress multi-site backend admin area
- Excerpt isn’t working or am I missing something?
- Can I temporarily disable global $post?
- Shortcode for pulling specific Post Title outside loop when ID is passed in
- WordPress loop only displays 5 articles max?
- How get the 10 most viewed pages (not post)
- Paged homepage – requirements?
- Least Number of Loops to Create Custom Homepage?
- The Loop for my theme with static front page is not working
- Prevent duplicating specific column from database table
- wp_mail function not working in user query loop
- How to set and retrieve cookies while in the loop