I have four post types
- Products
- Banners
- Portfolio
- Testimonials
And they all display their posts on the same home page template (index.php).
Currently using below query to get posts from different post types.
<?php
query_posts(
array(
'post_type' => 'work_projects',
'work_type' => 'website_development',
'posts_per_page' => 100
)
);
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_post_thumbnail($size);
the_title();
the_content();
endwhile; endif;
But my problem is I have to put multiple query_post
loops on the same page. I googled some solutions and found answers as well here on Stack Overflow also. But not able to figure it out how to use them with the_title()
, the_content()
and the_post_thumbnail()
.
I have four post types
- Products
- Banners
- Portfolio
- Testimonials
And they all display their posts on the same home page template (index.php).
Currently using below query to get posts from different post types.
<?php
query_posts(
array(
'post_type' => 'work_projects',
'work_type' => 'website_development',
'posts_per_page' => 100
)
);
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_post_thumbnail($size);
the_title();
the_content();
endwhile; endif;
But my problem is I have to put multiple query_post
loops on the same page. I googled some solutions and found answers as well here on Stack Overflow also. But not able to figure it out how to use them with the_title()
, the_content()
and the_post_thumbnail()
.
2 Answers
Reset to default 4post_type
accepts an array.
Have you tried using WP_Query
with an array?
$args = array(
'post_type' => array( 'product', 'banner', 'portfolio', 'testimonial'),
'post_status' => 'publish',
'posts_per_page' => 100
);
$the_query = new WP_Query( $args );
Best practice would be to use four different custom queries and loop through each one separately using wp_query
So here's an example:
<?php
$custom_query = new WP_Query(
array(
'post_type' => 'work_projects',
'work_type' => 'website_development',
'posts_per_page' => 100
)
);
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
the_post_thumbnail($size);
the_title();
the_content();
endwhile; endif; wp_reset_query();
?>
And then you would repeat the process for each post type, replacing 'work_projects' with the name of the next post type.
I'm not sure what 'work_type' is in this scenario, but I just put it back into the query since you already had it there.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744788217a4593756.html
评论列表(0条)