0

私のサイトのホームページでは、最新の投稿を最大にしてから、古い投稿を小さくしてその下に表示したいと考えています。画像を見る ここに画像の説明を入力

部分的に仕事をするワードプレスループを作成しました。ズームアウトして、より明確なビューを取得できるようにしました。 ここに画像の説明を入力

<?php if (have_posts()): ?>

  <section class="latest-blog">
<?php query_posts('showposts=5'); ?>

<?php $i = 0; while (have_posts()) : the_post(); ?>

  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <!-- Post Title -->
    <h1>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
    <!-- /Post Title -->
    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <br class="clear">

    <?php edit_post_link(); ?>

   </article>
 </section>

<section class="archive">
<?php if(++$i === 1):  ?>
<?php endif; ?>      

<?php endwhile; ?>
</section>    

<?php endif; ?>

起こっているように見えるのは、すべての古い投稿を記事としてセクションアーカイブ内に配置したいので、各古い投稿がセクションアーカイブに与えられることです。

4

4 に答える 4

2

私があなたの質問を理解していれば、複数のループは必要ないと思います。むしろ、ループで「特別な」ケースを使用して最初の最新の投稿を処理し、古い投稿をすべて通常どおりに処理できると思います (逆にしようとしているような?)。

これはどう:

  <?php 
  $firstPost = true; 
  query_posts('showposts=5');
  while (have_posts()) {
     the_post();
     if ($firstPost) {
       ?>
         <section class="latest-blog">
            my_article();
         </section><!-- /latest-blog -->

         <section class="archive">

     <?php 
       $firstPost = false;
     } // end of if(firstPost)
     ?>

     my_article();

   <?php
   } // end of the loop
   ?>

</section><!-- /archive -->

<?php
function my_article() {
   ?>
   <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <!-- Post Title -->
        <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
      <!-- /Post Title -->
      <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
      <br class="clear"> 
      <?php edit_post_link(); ?>  
   </article>
   <?php
}
?>

データの観点から、投稿がすべて同じである場合、それらを取得するために個別のクエリを実行することを考える本当の理由はありません。最初のものを別の方法で提示するだけです。そうすることでコードが削減され、エラーの発生する場所が減り、DB オーバーヘッドが削減され、サイトのパフォーマンスが向上します。

また、コーデックスはquery_posts()、これがあなたがしていることを行うための効率的な方法ではないことを示唆していることにも注意してください。したがって、これがそのまま機能するようになったら、pre_get_postsアクションを使用する WP 推奨のアプローチを調査することをお勧めしますが、これが「ページ」である場合には適用/適切ではない可能性があります。

于 2013-04-09T14:43:43.850 に答える
1

コードを 2 つのループに分割します。

注目の投稿の最初のループ:

<?php query_posts('showposts=1'); ?>
<section class="latest-blog">
<?php $i = 0; while (have_posts()) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <!-- Post Title -->
    <h1>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
    <!-- /Post Title -->
    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <br class="clear">

    <?php edit_post_link(); ?>

   </article>
<?php endwhile; ?>
</section>

そして、残りの投稿の 2 番目のループ:

<?php wp_reset_query(); ?>
<?php query_posts('showposts=5&offset=1'); ?>
<section class="archive">
<?php $i = 0; while (have_posts()) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <!-- Post Title -->
    <h1>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
    <!-- /Post Title -->
    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <br class="clear">

    <?php edit_post_link(); ?>

   </article>
   <?php endwhile; ?>
</section>

クエリで offset=1 を使用して、最初の投稿を 2 番目のループからオフセットしていることに気付くでしょう (2 回表示されないようにするため)。

最終的なコードは次のようになります。

<?php if (have_posts()): ?>
<?php query_posts('showposts=1'); ?>
<section class="latest-blog">
<?php $i = 0; while (have_posts()) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <!-- Post Title -->
    <h1>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
    <!-- /Post Title -->
    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <br class="clear">

    <?php edit_post_link(); ?>

   </article>
<?php endwhile; ?>
</section>

<?php wp_reset_query(); ?>
<?php query_posts('showposts=5&offset=1'); ?>
<section class="archive">
<?php $i = 0; while (have_posts()) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <!-- Post Title -->
    <h1>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
    <!-- /Post Title -->
    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <br class="clear">

    <?php edit_post_link(); ?>

   </article>
   <?php endwhile; ?>
</section>
<?php endif; ?>
于 2013-04-09T14:43:54.320 に答える