1

4つのテンプレートパーツをランダムな順序で表示する方法を探しています。それらは2x2グリッドに配置されており、ページが読み込まれるたびに順序を変える必要があります。コードは次のとおりです。

<!-- Feature 1 -->
      <div class="grid_half">
        <?php get_template_part( 'features/feat','1' ); //Get Features Section 1 ?>
    </div><!-- /.grid_half -->
<!-- Feature 2 -->
      <div class="grid_half_last"> 
        <?php get_template_part( 'features/feat','2' ); //Get Features Section 2 ?>
     </div><!-- /.grid_half_last -->
      <div class="clear"></div>
<!-- Feature 3 -->
      <div class="grid_half"> 
        <?php get_template_part( 'features/feat','3' ); //Get Features Section 3 ?>
      </div><!-- /.grid_half -->
<!-- Feature 4 -->
      <div class="grid_half_last">
        <?php get_template_part( 'features/feat','4' ); //Get Features Section 4 ?>
      </div><!-- /.grid_half_last -->
      <div class="clear"></div>
4

2 に答える 2

2
$order = array(1, 2, 3, 4);
shuffle($order);

それで:

get_template_part( 'features/feat', array_shift($order) );

4つの連続するページリクエスト内で同じ順序を取得しないようにするには、この配列、おそらくトラフセッションまたはクライアントCookieを追跡する必要があります。

于 2013-02-17T00:04:25.407 に答える
1

これは私のために働いているようです:

    <?php $features = array('1', '2', '3', '4'); 
        shuffle($features); ?>
<!-- Feature 1 -->
      <div class="grid_half">
        <?php get_template_part( 'features/feat', $features[0] ); //Get Features Section 1 ?>
    </div><!-- /.grid_half -->
<!-- Feature 2 -->
      <div class="grid_half_last"> 
        <?php get_template_part( 'features/feat', $features[1] ); //Get Features Section 2 ?>
     </div><!-- /.grid_half_last -->
      <div class="clear"></div>
      <br />
      <br />
      <!-- Feature -->
      <div class="grid_half"> 
        <?php get_template_part( 'features/feat', $features[2] ); //Get Features Section 3 ?>
      </div><!-- /.grid_half -->
<!-- Feature -->
      <div class="grid_half_last">
        <?php get_template_part( 'features/feat', $features[3] ); //Get Features Section 4 ?>
      </div><!-- /.grid_half_last -->
      <div class="clear"></div>

ファイルは、完全な情報のために、featuresという名前のサブフォルダーでfeat-1.php、feat-2.phpなどの名前が付けられています。

于 2013-02-17T00:22:32.170 に答える