Php 如何在Wordpress中显示默认帖子的无限帖子?
我正在努力开发一个Wordpress博客。我想博客将显示在一个页面无限的职位。帖子将来自默认的帖子项。为此,我使用了以下循环Php 如何在Wordpress中显示默认帖子的无限帖子?,php,wordpress,Php,Wordpress,我正在努力开发一个Wordpress博客。我想博客将显示在一个页面无限的职位。帖子将来自默认的帖子项。为此,我使用了以下循环 <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php the_title();?> <?php the_content();?> <?php endwhile; ?&
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
但是我如何在这里设置无限帖子的
-1
值呢。另外,管理员阅读设置选项中不支持-1
值。请告诉我解决方案。这将通过覆盖全局$wp\u查询
<?php global $wp_query ;
$args = array('post_per_page' => '-1');
$wp_query = new WP_Query($args) ; // overriding default global of WordPress
?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Don't forget to add this
?>
类似的东西应该可以工作(未经测试)。我不确定代码片段的上下文,但假设您正在实例化一个新的WP查询:
<?php
$args = ['posts_per_page' => -1];
$the_query = new WP_Query( $args );
?>
有关“循环内”实现,请参阅本手册:
确保wp_reset_postdata();为了安全起见。我建议您使用模板文件并编写一些自定义查询,以便在一页中显示所有帖子:
像这样:
创建一个模板文件
<?php
/* Template Name: All post
*/
$args = array( 'post_type' => 'post', 'posts_per_page' => -1)
$allpost = get_posts( $args );
foreach ( $allpost as $post ) {
setup_postdata( $post );
the_title();
the_content();
}
wp_reset_postdata();
?>
现在在“管理”中,选择此模板文件到要显示所有帖子的任何页面…我已找到解决方案
$my_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'paged' => $paged
);
$my_query = new WP_Query( $my_args );
if($my_query->have_posts()) : ?>
<?php while ($my_query->have_posts() ) : $my_query->the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
$my_args=数组(
“post_type”=>“post”,
“每页帖子数”=>-1,
“paged”=>paged美元
);
$my\u query=新的WP\u查询($my\u args);
如果($my\u query->have\u posts()):?>
什么意思,无限制的帖子?我想你想要懒洋洋的帖子?无限制的帖子意味着,所有的帖子都会显示在一个页面上。在pagePff中没有分页,对于20000篇文章,您的网站将。。。非常慢。分析错误:语法错误,意外的T\u双箭头