1

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!

4

3 回答 3

1

老兄解决这个问题:

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

剩下的就是你的代码......没有变化。希望它会起作用

于 2013-08-02T11:16:26.027 回答
0
<?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;
?>

辅助函数必须在您的functions.php文件中,并且它们必须echo简单地使用模板标签(the_content()、the_title() 等)来处理您的 HTML;我想你不是在要求整个 HTML+CSS 布局,对吧?

显然,您可以将 HTML 与 PHP 混合......这不是很好,但出于测试目的,它就可以了。

于 2013-07-03T22:13:48.550 回答
0

设计参数略有变化。我想出了一个正在努力展示的解决方案:

1 个全宽新闻项目

3条新闻摘录

1 个全宽音乐项目

16 个带有图像和标题的音乐项目

3 个来自杂项类别的帖子

来自不同杂项类别的 3 个帖子

对于我使用的每个部分的内容-get_template_part。

这是有效的:

从一个循环开始以显示第一个全角新闻项目:

<?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; ?>

使用第二个循环显示接下来的 3 个新闻项目。偏移量是跳过已显示在 中的第一个新闻项的键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; ?>

下一节使用不同的分类术语重复上述循环。

<?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; ?>

最后一部分是来自分类术语的另外两个循环。

<?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; ?>
于 2013-08-09T04:19:12.060 回答