1

WordPress でランディング ページ (多くのセクションを含む 1 つのページ) を作成しており、get_template_part() を使用して各セクションを呼び出しています。だから私はこのようなものを持っています;

    <?php get_template_part( 'content', 'reasons-to-purchase'); ?>   
    <?php get_template_part( 'content', 'testimonials'); ?>  
    <?php get_template_part( 'content', 'reasons-to-purchase'); ?>   

各コンテンツ ファイルには、次に示すように、posts_per_page を 1 に設定して、そのカテゴリに一致する投稿を返すループがあります (content-reasons-to-purchase.php)。

            <?php

        $args = array(
            'post_type' => 'section',    
            'category_name' => 'Reasons-To-Purchase',
            'posts_per_page' => 1,
            'orderby' => 'date',
            'order' => 'DESC'
        );

        $the_query = new WP_Query ( $args );

 if (get_category_by_slug('Reasons-To-Purchase')->category_count > 0 ) {?>


<!-- Featured content image or slider. -->
        <div class="container panel">

            <?php if ( have_posts() ) : while ( $the_query->have_posts () ) : $the_query->the_post(); ?>

           <?php the_content(); ?>


        </div>

<?php } ?>

            <?php wp_reset_query(); ?> <div class="clearfix"></div>    

私が望むのは、好きな順序でこれらのセクションをいくつでも作成し、各セクション内のループがそのカテゴリに割り当てられた次の投稿をプルすることです。たとえば、最初の「購入理由」セクションで最初の投稿がプルされ、2 番目の「購入理由」セクションで 2 番目の投稿が呼び出されます。現時点では、「wp_reset_query()」を使用して各ファイルのループをリセットしているため、異なる可能性のある次のセクションに干渉しません。基本的に、ループは、投稿を複製することなく、次の同様の名前付きセクションに進む必要があります..

これを行う方法やアドバイスについてのアイデアは大歓迎です。

4

1 に答える 1

1

呼び出す前にページ パラメータを渡す必要がありますが、変数の再利用はサポートされていません。locate_template() と include() を使用する必要get_template_part()ありますget_template_part()

$page=1;
include(locate_template('content-reasons-to-purchase.php'));
include(locate_template('content-testimonials.php'));
$page=2;
include(locate_template('content-reasons-to-purchase.php'));
$page=3;
include(locate_template('content-reasons-to-purchase.php'));/* and so on page 4,5,6... */

テンプレートで、コードをわずかに変更します

if(empty($page)){
$page=1;
}

 $args = array(
            'post_type' => 'section',    
            'category_name' => 'Reasons-To-Purchase',
            'posts_per_page' => 1,
            'orderby' => 'date',
            'paged'=>$page, /* <======= get page no from your file i.e locate_template(...) and use here*/
            'order' => 'DESC'
        );

それが理にかなっていることを願っています

于 2013-11-17T15:39:25.367 に答える