0 レビュー
1 回答
php-Wordpressブログの最初の行2列2番目以降の行3列
Bootstrapを使用してWordpressアーカイブブログテンプレートを作成したいと思います。
最初の行には、最初の投稿を8列、2番目の投稿を4列にする必要があります。
2行目以降の行には、4列の投稿が必要です。
phpカウンターでこのテンプレートが有効になると思います。誰かがコードの書き方を知っていますか?
わからない
0
レビュー
答え :
解決策:
get_posts()
を使用してすべての投稿を取得することから始めて、投稿を取得します。これにより、次の配列が返されます。 WP_Post
オブジェクト。次に、次のように投稿をループします:
// Get all our posts, and start the counter
$postNumber = 0;
$args = array(
'posts_per_page' => 8
);
$posts = get_posts($args);
// Loop through each of our posts
foreach ($posts as $post) {
// If we're at the first post, or just before a post number that is divisible by three then we start a new row
if (($postNumber == 0) || (($postNumber+1) % 3 == 0)) {
echo '<div class="row">';
}
// Choose the post class based on what number post we are
$postClass = '';
if ($postNumber == 0) {
$postClass .= "col-md-8";
} else {
$postClass .= "col-md-4";
}
echo '<div class="'.$postClass.'">';
// Print the post data...
echo $postNumber. " " . $postClass;
echo "</div>";
// If we are at the second post, or we're just after a post number divisible by three we are at the end of the row
if (($postNumber == 1) || (($postNumber-1) % 3 == 0)) {
echo '</div>'; // close row tag
}
$postNumber++; // Increment counter
}
これにより、次のような出力が得られます:
テンプレートに基づいてこれを変更する必要があることは明らかですが、これは良い出発点になるはずです。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。