I’m trying to make a custom template to display multiple loops from the same custom post type, but different categories.
Here’s what I am after:
From custom post type: ‘Portfolio’
In custom category 1 ‘Music’:
- 1 featured post at top
- Music Heading
- 3 sub-featured posts
- 12 posts (title only)
In custom category 2 ‘Presenters’:
– Presenters Heading
– 3 posts
In custom category 3 ‘News’:
– News Heading
– 3 posts
Here’s the code I am working with:
<?php if (have_posts()) : while (have_posts()) : the_post(); //WP loop ?>
<?php the_content(); ?>
<?php $args=array( //Loop 1
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 16
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end music loop ?>
<h2>Presenters</h2>
<?php $args=array( //Loop 2
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'presenters',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end presenters loop ?>
<h2>News</h2>
<?php $args=array( //Loop 3
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'news',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end news loop ?>
<?php endwhile; endif; // end WP loop?>
Overall the 3 loops work great.
The part I need help on is the 1st loop section. I need to take all 16 posts from the same custom taxonomy ‘dt_portfolio_category’ -> ‘music’. But break them into a 1 top featured post (full-width), then a heading, then 3 sub-featured posts (3 columns), then 12 posts with just the title (3 columns). I have tried to break it into 3 separate loops, but the content gets duplicated… and I figure there must be a cleaner way to do it.
Thank You!
dude work out with this:
rest is with your code… no changes.
Hope it will work
The helper functions have to be in your
functions.php
file and they have toecho
-ing simply your HTML with template tags (the_content(), the_title(), etc…); I think you are not asking for the entire HTML+CSS layout, right?Obviously you can put the HTML mixed with the PHP…that is not such good, but for testing purpose it is just ok.
The design parameters have changes slightly. I have come up with a solution that is working to show:
1 full width news item
3 news with excerpt
1 full width music item
16 music items with image and title
3 posts from a misc category
3 posts from a different misc category
For the content in each section I am using- get_template_part.
Here’s what is working:
Start with one loop to show the 1st full width news item:
Use a second loop to show the next 3 news items. Offset is the key for skipping the first news item that has already been displayed in the
fullnewsloop
.The next section recycles the above loops using different taxonomy terms.
The last section is two more loops from taxonomy terms.