0

私がやろうとしていること:

ページの 1 番目、2 番目、3 番目の投稿に対して異なる HTML マークアップを表示するために、ループ内に elseif ステートメントを記述します。

コード

  <div id="content">

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

            <?php $count = 0; ?> 
            <?php while (have_posts()) : the_post(); ?> 
            <?php $count++; ?> 
            <?php if ($count == 1) : ?> 


            <!-- this is the 1st post -->

            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

                //the html for the first post
                <?php the_title(); ?> //etc

            </article> <!-- end div post -->


            <?php elseif : ?>
            <?php if ($count == 2) : ?>


            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                //second post                   
                <?php the_title(); ?> //etc


            </article> <!-- end div post -->


            <?php elseif : ?>
            <?php if ($count == 3) : ?>


            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                //third post                    
                <?php the_title(); ?> //etc


            </article> <!-- end div post -->


            <?php else : ?>

        <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
            all the other posts     
            <?php the_title(); ?> etc


        </article> <!-- end div post -->


    <?php endif; ?> 
    <?php endwhile; ?>
    <?php endif; ?>


    </div> <!-- end div content -->

実際に起こっていること:

何もない。私は明らかに間違っています。コードにコメントを付けたので、ここで何を達成しようとしているのかがわかります。おそらく、あなたは私を正しい方向に向けることができます!

ここにいくつかの作業コードがあります。最初の投稿には他の投稿とは異なるマークアップがあります。

<div id="content">

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

                    <?php $count = 0; ?> 
                    <?php while (have_posts()) : the_post(); ?> 
                    <?php $count++; ?> 
                    <?php if ($count == 1) : ?> 


                    <!-- this is the 1st post -->

                    <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

                        //the html for the first post
                        <?php the_title(); ?> //etc

                    </article> <!-- end div post -->





                    <?php else : ?>

                <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                    all the other posts     
                    <?php the_title(); ?> etc


                </article> <!-- end div post -->


            <?php endif; ?> 
            <?php endwhile; ?>
            <?php endif; ?>


            </div> <!-- end div content -->

これを拡張して、1 番目、2 番目、3 番目の投稿で異なる html を使用したいだけです。

4

1 に答える 1

1

問題

 <?php elseif : ?>
 <?php if ($count == 3) : ?>

あるべきだ

elseif($count == 3) :

より良い方法

get_template_partを使用して、ネストされた複雑さを軽減できます。articlepart.php や articlepart-1.php などのテンプレート用に個別に作成されます。

if(have_posts()):
$count = 0;
    while (have_posts()) : the_post();
        $count++;
        get_template_part('articlepart', $count);

    endwhile;
endif;
于 2013-08-08T06:29:29.643 に答える