0

私は探していたものの答えを見つけようとしましたが、これに対する答えはまだ見ていません:

1 つのカテゴリからすべての投稿を取得し、<li></li>タグ内に一度に 3 つ表示する Wordpress ループを生成しようとしています。

出力は次のようになります。

<li>My post title | Another Title | Third title</li>
<li>The next post title | A different post | Post #6</li>
<li>And so on | And so forth</li>

終了するまでカテゴリ内のすべてのエントリをループし、ループを終了するには、これが必要です。

私のコードはこの時点で完全に機能していませんが、以下に作業中のものを提供しました。誰かがこれに対する解決策を持っている場合、私はあなたに狂った小道具を贈りたい.

<?php // Loop through posts three at a time
$recoffsetinit = '0';
$recoffset = '3';
query_posts('cat=1&showposts=0');
$post = get_posts('category=1&numberposts=3&offset='.$recoffsetinit.');
while (have_posts()) : the_post(); 
?>
<li>
<?php
$postslist = get_posts('cat=1&order=ASC&orderby=title');
foreach ($postslist as $post) : setup_postdata($post);
 static $count = 0; if ($count == "3") { break; } else { ?>
 <a href="<?php the_permalink() ?>"></a>
<?php $count++; } ?>
<?php endforeach; ?>
<?php $recoffsetinit = $recoffset + $recoffsetinit; ?>
</li>
<?php endwhile; ?>
4

3 に答える 3

1

私はそれを機能させるためにあなたのソリューションをハックしました。私のコードフーはあなたが「良い」と呼ぶものではないので、少し手間がかかりました。解決策は次のとおりです。

<ul>
<?php
query_posts('category=1&showposts=0');
$posts = get_posts('category_name=my_cat&order=ASC&orderby=title&numberposts=0'); 
$postsPerLine = 3;
$currentPostNumber = 0;

foreach ($posts as $post) :

    if ($currentPostNumber == 0) {
            echo '<li>';
    }
            ?>

    <a href="<?php the_permalink(); ?>"></a>

    <?php
$currentPostNumber++;

    if ($currentPostNumber >= $postsPerLine) { 
            $currentPostNumber = 0;
            echo '</li>';
    }

  endforeach;
  ?>
</ul>

入力していただきありがとうございます!

于 2009-06-11T16:30:09.990 に答える
0

テストするワードプレスも時間もありませんが、このようなものがそれを実行するためのより良い方法かもしれませんか?

<?php

$postList = get_posts('cat=1&order=ASC&orderby=title');
$postsPerLine = 3;

echo "<ul>";
echo buildPosts($postList, $postsPerLine);
echo "</ul>";

function buildPosts($list, $perLine) {

    $out = '';
    $currentPostNumber = 0;

    foreach ($list as $post) {

        if ($currentPostNumber == 0) {
            $out .= '<li>';
        }

        $out .= "<a href='" . the_permalink() . "'></a> ";

        $currentPostNumber++;

        if ($currentPostNumber <= $perLine) { 
            $currentPostNumber = 0;
            $out .= '</li>';
        }

    }
    return $out;
}

?>
于 2009-06-10T23:22:14.077 に答える
0

カテゴリのすべての投稿を一度に取得してから、繰り返します。すべての投稿へのリンクを作成し、セパレーターに入れて、3つおきの投稿で新しい投稿を開始します<li>

<ul>
<?php
global $post;
$postsPerLine = 3;
$counter = 0;
$myposts = get_posts('category=1&orderby=title&order=ASC');

foreach($myposts as $post) :
    echo (++$counter % postsPerLine) ? : '<li>';
?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
    echo ($counter % postsPerLine) ? ' | ' : '</li>';
endforeach;

?>
</ul>
于 2009-07-09T06:44:09.323 に答える