【发布时间】:2016-01-13 01:25:31
【问题描述】:
我需要为我的博客 home.php 创建自定义多个循环,以显示定义类别的 3 种不同内容布局,然后继续进行一般循环,前 3 个循环中的帖子 ID 除外。
到目前为止,我制作了定义类别category_name=arts的3种不同内容布局:
<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count3++; } ?>
<?php endforeach; ?>
我确实卡住了继续进行一般循环。任何建议都非常感谢。
【问题讨论】:
-
你的代码非常慢而且非常糟糕。您应该完全删除
query_posts,它会破坏主查询对象。使用get_posts或WP_Query。从那里继续。您还将每个循环运行两次。 -
@PieterGoosen 你能分享示例代码吗?