1

簡単なクエリがあります。$query = new WP_Query('showposts=5');これは、明らかに 5 つの最新の投稿を表示します。クエリで投稿の位置を取得できる方法はありますか? つまり、$query には 5 つの投稿があり、ループ内の投稿の数を表示できるようにする必要があります。PHP はよくわかりませんが、$query は配列変数 (?) であり、これらの 5 つの投稿があると思います。

これは、JavaScript スライダーで使用されます。ここでは、すべての投稿に対して次のようなリンクを表示<a href="#1"></a>します。2 番目の投稿では 2、3 番目の投稿では 3 にする必要があります。

それが理にかなっており、誰かが私を助けてくれることを願っています。

前もって感謝します、ジャスティン

4

3 に答える 3

4

the_ID()防弾動作を強化するには、ページ上の位置ではなく、各投稿の UID (を使用) を使用してアンカー リンクを作成します。

さらに、ループ$queryを使用して繰り返し処理する必要があります。これは、ページで複数回実行できます。(Wordpress をしばらく使用していないため、このコードは少しずれている可能性がありますが、コンセプトは適切です)

<?php 

// Create the Query
$query = new WP_Query('showposts=5');

if ($query->have_posts()) :

    // Create the Javascript slider thing
    while ($query->have_posts()) : $query->the_post();
        // Do stuff here
    endwhile; 

    // Display the posts
    while ($query->have_posts()) : $query->the_post();
        // Do stuff here
    endwhile;

endif;

?>
于 2009-09-03T13:00:22.690 に答える
2

$query->current_post は、ループ内の現在のアイテムのインデックスを提供します。また、 $query->post_count は、ループ内のアイテムの合計数を示します。それも役立つかもしれません。

于 2009-11-02T02:01:44.173 に答える
1

それほど難しいことではありません (私は cpharmston の PHP コードをコピーしました):

<?php 

// Create the Query
$query = new WP_Query('showposts=5');

if ($query->have_posts()) :

    $i = 1; // for counting

    // Create the Javascript slider thing
    while ($query->have_posts()) : $query->the_post();
        // Do stuff here
        $i++; // make sure this is @ the end of the while-loop
    endwhile;


    $i = 1; // reset $i to 1 for counting

    // Display the posts
    while ($query->have_posts()) : $query->the_post();
        // Do stuff here
        $i++; // make sure this is @ the end of the while-loop
    endwhile;

endif;

?>

#1、#2 などに $i を使用できます。while ループが終了するたびに、$i++ は必ず 1 ずつインクリメントします (最初の $i = 2 などの後)。

これが役立つことを願っています:)(ただし、cpharmstonのソリューションの方が簡単だと思います)。

于 2009-09-03T13:17:32.117 に答える