這是我擁有的標準HTML代碼,我試圖將它合併到一個基本上以兩種不同方式顯示帖子的Wordpress循環中。我使用更多的字段插件爲用戶選擇顯示帖子的兩種方式之一。 1)如果他們選擇Large,那麼帖子會被div類的「大鏈接」所包裹。2)如果用戶選擇「Small」,則會創建一個類爲「groupOflinks」的div,並且該帖子被div類「smallLink 」。只有4個'smallLink'div/posts可以放在一個'groupOflinks'div內。如果有超過4個小帖子,則創建一個新的「groupOflinks」div,並重復該過程。Wordpress嵌套循環
下面是HTML代碼我試圖合併(大量註釋):
<!-- Display one post within this container only & if there are more posts that have been chosen as Large, wrap them in the 'largeLink' div -->
<div class="largeLink">
<!-- post 1 content here -->
</div>
<div class="largeLink">
<!-- post 2 content here -->
</div>
<!-- If a post is defined as small within the admin panel (using more fields plugin)
display them within this 'groupoflinks' container
If 4 posts are already in this 'groupoflinks' container create a new container and populate with another 4 posts
Repeat -->
<div class="groupOfLinks">
<!-- This is a container that holds 4 posts only & if there are more posts that have been assigned as 'Small' create a new 'groupOflinks' container and populate with the next 4 -->
<div class="smallLink">
<!-- Post 4 content here -->
</div>
<div class="smallLink">
<!-- Post 5 content here -->
</div>
<div class="smallLink">
<!-- Post 6 content here -->
</div>
<div class="smallLink">
<!-- Post 7 content here -->
</div>
</div>
<div class="largeLink">
<!-- post 3 content here -->
</div>
<div class="largeLink">
<!-- post 8 content here -->
</div>
這是我的WP循環到目前爲止這完全不是那麼回事:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // Take the value defined with the 'Layout' field and change style accordingly
$layouttype = get_post_meta($post->ID, 'layout-type', true) ?>
<?php if ($layouttype == 'Small') { ?>
<div class="groupOfLinks">
<!--LOOP STYLE 1 GOES HERE-->
<?php $temp_query = $wp_query; // store it
$args = array(
'paged' => $paged, // paginates
'post_type'=>'post',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();?>
<div class="smallLink">
<!-- Post content here -->
<h1><?php the_title(); ?></h1>
<?php the_content(''); ?>
</div>
<?php if (isset($wp_query)) {$wp_query = $temp_query;} // restore loop
endwhile; ?>
</div>
<?php } else { ?>
<!--LOOP STYLE 2 GOES HERE-->
<div class="largeLink">
<!-- post 1 content here -->
<h1><?php the_title(); ?></h1>
<?php the_content(''); ?>
</div>
<?php } ?>
<?php endwhile; ?>
<div>
<div><?php next_posts_link('« Older Entries') ?></div>
<div><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn’t here.</p>
<?php endif; ?>
只是爲了澄清,你想要兩個大帖子,然後是一組4小帖子,然後重複,直到你完成你的帖子? –
你想要它的任何方式,我有一個解決方案,只是澄清最終目標。 –
他們進來的順序並不重要,儘管最好按照日期順序。最新的帖子第一,最舊的帖子最後。因此,根據他們添加的日期,我可以首先顯示一個4個郵政集裝箱,然後是5個(例如)大型單個職位,然後是另一個小型郵政集裝箱。或者我可以有一個大的單一職位,然後是3個小型郵政集裝箱。我希望它在外觀上有所不同,取決於每篇文章的發佈時間。我希望這是有道理:) – egr103