1

同じカスタム投稿タイプの複数のループを表示するカスタム テンプレートを作成しようとしていますが、カテゴリは異なります。

これが私が求めているものです:

カスタム投稿タイプから: 「ポートフォリオ」

カスタム カテゴリ 1 の「音楽」:

  • 注目の投稿 1 件を一番上に表示
  • 音楽見出し
  • 3 つのサブ特集記事
  • 12 投稿 (タイトルのみ)

カスタム カテゴリ 2「発表者」: - 発表者の見出し - 3 件の投稿

カスタム カテゴリ 3 'ニュース': - ニュースの見出し - 3 件の投稿

ここに私が取り組んでいるコードがあります:

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

全体として、3 つのループはうまく機能します。

ヘルプが必要な部分は、1 番目のループ セクションです。同じカスタム分類 'dt_portfolio_category' -> 'music' から 16 の投稿すべてを取得する必要があります。ただし、それらを 1 つのトップ特集記事 (全幅)、見出し、サブ特集記事 (3 列) 3 つ、タイトルだけの 12 記事 (3 列) に分割します。私はそれを3つの別々のループに分割しようとしましたが、内容が重複しています...そして、それを行うためのよりクリーンな方法があるに違いないと思います.

ありがとう!

4

3 に答える 3

0

設計パラメータはわずかに変更されています。以下を示すために機能するソリューションを思いつきました。

全角ニュース1件

3 ニュース抜粋

1 全幅音楽アイテム

画像とタイトル付きの16曲の音楽アイテム

その他のカテゴリからの 3 件の投稿

別のその他のカテゴリからの 3 件の投稿

私が使用している各セクションのコンテンツについては、get_template_part.

これが機能しているものです:

最初の全幅のニュース項目を表示する 1 つのループから始めます。

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

2 番目のループを使用して、次の 3 つのニュース項目を表示します。Offset は、 に既に表示されている最初のニュース項目をスキップするためのキーです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; ?>

最後のセクションは、分類用語からの 2 つのループです。

<?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 に答える