在单独页面上向 WordPress 主题添加第二个循环
我试图在两个单独的页面上向主题添加两个循环:主页和博客。
博客基本上是帖子的索引。这是大多数 WordPress 页面默认的主页。为了实现这一点,我进入“阅读设置”并将“首页显示”设置为“静态”,将“首页”设置为我在 WordPress 页面中设置的主页,将“帖子页面”设置为博客页面。
现在的问题是,当我将循环添加到主页时,它不起作用,大概是因为我将帖子页面设置为不同的页面。
那么如何让循环在主页和博客页面上工作呢?顺便说一句,主页循环只是帖子标题+日期+也许是摘录。我是否需要完全重新设计主题,或者这在 WordPress 下是不可能的?
哦,我使用的循环是:
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post() ?>
I'm trying to add two loops to a theme on two separate pages: home and blog.
Blog is basically an index of the posts. It's what most Wordpress pages default to as a home page. To accomplish this I went to "reading settings" and set "front page displays" as 'static' with "front page" set to a Home page I set up in Wordpress pages and "posts page" set to a Blog page.
Now the problem is that when I add the loop to the Home page, it doesn't work, presumably because I have posts page set to a different page.
So how do I get the loop to work on the Home page as well as the blog page? Btw, the Home page loop is just post title + date + maybe excerpts. Do I need to completely rework the theme or is this is just not a possibility under Wordpress?
Oh and the loop I'm using is:
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post() ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 WordPress 中运行自定义查询至少有三种方法。
Query_posts() 可以定义第二个循环的查询字符串。这很容易而且很常见。此代码是我从 query_posts() 的法典页面复制的基本结构:
您也可以使用类似的 get_posts() 。
这两个函数都接受许多参数,这些参数在 query_posts 函数参考页上进行了解释。上面显示的参数仅是示例。可用参数的列表很长。
您可以使用的第三种方法是实例化 WordPress 查询对象(WP 的主要查询方法)的另一个实例)。在 WordPress 运行默认的 wp_query 后,Query_posts 和 get_posts 都会对数据库进行第二次调用。如果您非常关心性能或减少数据库命中,我建议您学习如何与 wp_query 交互以在运行之前修改默认查询。 wp_query 类提供了许多简单的方法供您修改查询。
祝你好运!
There are at least three wayst to run custom queries in WordPress.
Query_posts() can define the query string of your second loop. It is easy and very common to do. This code is a basic structure I copied from the codex page for query_posts():
You can also use get_posts() which is similar.
Both functions accept a number of arguments that are explained on the query_posts function reference page. The arguments shown above are only examples. The list of available args is long.
A third method available to you is to instantiate another instance of the WordPress Query object (WP's main query method). Query_posts and get_posts both run a second call to the database after WordPress runs the default wp_query. If you are super concerned about performance or reducing db hits, I suggest learning how you can interact with wp_query to modify the default query before it is run. The wp_query class provides a number of simple methods for you to modify the query.
Good Luck!
WordPress 可能不会为您启动循环,因为您使用静态页面。但是如果这个静态页面是在你的主题中定义的(因为你包含了显示循环的 PHP 代码,我假设是这样),你总是可以 自己在那里开始一个新循环。只需自己调用
query_posts
,您的代码就应该开始工作。It is possible that WordPress does not start a loop for you because you use a static page. But if this static page is defined in your theme (since you include the PHP code to display the loop, I assume it is), you can always start a new loop there yourself. Just call
query_posts
yourself, and your code should start working.