简体   繁体   English

Wordpress限制主页上的帖子

[英]Wordpress Limit posts on home page

I am developing a wordpress blog but for some reason the posts won't limit on the home page. 我正在开发一个wordpress博客,但由于某种原因,帖子不会限制在主页上。 I am trying to limit to 3 but it falls back to what is set in the administration area instead of 2. How come? 我试图将其限制为3,但它会回落到管理区域中设置的而不是2.为什么?

What am I doing wrong? 我究竟做错了什么?

<?php
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query('posts_per_page=-1&paged=' . $paged);
        query_posts('showposts = 2');
        $flag = 1;
        while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>

Try using this code in the functions.php to limit the posts on the homepage, it should limit the posts to 2: 尝试在functions.php中使用此代码来限制主页上的帖子,它应该将帖子限制为2:

function posts_on_homepage( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 2 );
    }
}
add_action( 'pre_get_posts', 'posts_on_homepage' );

There are 2 option available to do your task, you can easily limit post on your home page. 有2个选项可用于执行您的任务,您可以轻松限制主页上的帖子。

1) From the Dashboard: 1)从仪表板:

(a) Settings-> (a)设置 - >

(b) Reading -> (b)阅读 - >

(c) Blog Posts - set to 3 (c)博客帖子 - 设置为3

Or you can do this.... 或者你可以这样做....

2) From the Dashboard: 2)从仪表板:

(a) Appearance (a)外观

(b) Editor (b)编辑

(c) home page file (usually index.php) (c)主页文件(通常是index.php)

(d) Locate this line: (d)找到这一行:

(e) Just before this line add this line: (e)在此行之前添加以下行:

I hope this process will work for you, if you need more help I'm always available to help you 我希望这个过程对您有用,如果您需要更多帮助,我随时可以为您提供帮助

You cannot use WP_Query and query_posts together. 您不能一起使用WP_Queryquery_posts And you should never use query_posts . 你永远不应该使用query_posts You should either use pre_get_posts or WP_Query . 您应该使用pre_get_postsWP_Query

You should set posts_per_page to 3 if you need to display 3 posts. 如果需要显示3个帖子,则应将posts_per_page设置为3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM