Question

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!

Was it helpful?

Solution 3

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:

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'news',
    'posts_per_page' => 1                
);

$fullnewsloop = new WP_Query($args);

if($fullnewsloop->have_posts()) : while($fullnewsloop->have_posts()) :
    $fullnewsloop->the_post();

    get_template_part( 'content-full-width', get_post_format() );
endwhile; endif; ?>

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.

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'news',
    'posts_per_page' => 3,
    'offset' => 1 // this skips the first post from the news category.
);

$shortnewsloop = new WP_Query($args);

if($shortnewsloop->have_posts()) : while($shortnewsloop->have_posts()) :
    $shortnewsloop->the_post();                

    get_template_part( 'content-title-excerpt', get_post_format() );
endwhile; endif; ?>

The next section recycles the above loops using different taxonomy terms.

<?php
$args=array ( 
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'music',
    'posts_per_page' => 1
);

$fullmusicloop = new WP_Query($args);

if($fullmusicloop->have_posts()) : while($fullmusicloop->have_posts()) :
    $fullmusicloop->the_post();

    get_template_part( 'content-full-width', get_post_format() );
endwhile; endif; ?>

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'music',
    'posts_per_page' => 16,
    'offset' => 1 // this skips the post already displayed in the fullmusicloop.
);

$shortmusicloop = new WP_Query($args);

if($shortmusicloop->have_posts()) : while($shortmusicloop->have_posts()) :
    $shortmusicloop->the_post();

    get_template_part( 'content-title-image', get_post_format() );
endwhile; endif; ?>

The last section is two more loops from taxonomy terms.

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'speakerss',
    'posts_per_page' => 3,
);

$speakersloop = new WP_Query($args);

if($speakersloop->have_posts()) : while($speakersloop->have_posts()) :
    $speakersloop->the_post();                

    get_template_part( 'content-title-image', get_post_format() ); 
endwhile; endif; ?>

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'artists',
    'posts_per_page' => 3,
);

$artistsloop = new WP_Query($args);

if($artistsloop->have_posts()) : while($artistsloop->have_posts()) :
    $artistsloop->the_post();

    get_template_part( 'content-title-image', get_post_format() );
endwhile; endif; ?>

OTHER TIPS

dude work out with this:

$args=array( //Loop 3
            'post_type' => 'dt_portfolio',
            'tax_query' => array(
                   array('taxonomy'=>'dt_portfolio_category',
                             'term'=> 'news',
                             'field'=>'slug')
            ),
            'posts_per_page' => 3
            );

rest is with your code... no changes. Hope it will work

<?php
$args=array(
    'post_type' => 'dt_portfolio',
    'taxonomy' => 'dt_portfolio_category',
    'term' => 'music',
    'posts_per_page' => 16
);

$music = new WP_Query($args);

$counter = 1;

if($music->have_posts()) : while($music->have_posts()) :
    $music->the_post();

    if ($counter = 1){
        # code...
        # I'd use some helper functions here
        print_main_music();
    } elseif ($counter > 1 && $counter < 5) {
        # code...
        # I'd use some helper functions here
        print_featured_music();
    } else {
        # code...
        # I'd use some helper functions here
        print_other_music();
    }

    $counter++;

endwhile; endif;
?>

The helper functions have to be in your functions.php file and they have to echo-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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top