wordpress 循环浏览自定义 php 页面中的帖子

wordpress looping through posts in custom php page

我试图循环浏览自定义 php 页面中的帖子,但无论我做什么,都找不到帖子 这是我在 my-custom-page.php

中写的代码
<?php 
require_once("/wp-load.php");
get_header();?>
<div id="blog">
<?php if(have_posts()) : ?>
 <?php echo"anything"; ?>
<?php endif; ?>
</div>
<?php get_footer();?>

wp_count_posts :
@return 对象每个状态的帖子数。

您正在尝试回显一个以致命错误结尾的对象。此外,如果你想看到所有帖子 the_post 是不对的。在函数参考中查找它:https://codex.wordpress.org/Function_Reference/the_post。我会做其他的(google smth 就像 "get all posts")。

您应该通过此文件的完整路径要求 wp-load.php。

硬编码示例:

require_once("user/home/public-html/wordpress/wp-load.php");

软编码示例(假设您的文件与 WordPress 在同一目录中):

require_once(dirname(__FILE__)."/wp-load.php");

您还必须在显示帖子之前对其进行查询。因此,您需要将这一行添加到您的代码中:

query_posts('post_type=post');

查询参数可能因您要显示的内容而异。其中一些是WP_Postclass的成员变量。转到 https://codex.wordpress.org/Class_Reference/WP_Post 以供参考。

这里有一个 re-writing 代码,显示了最近发布的 30 篇博文的标题:

<?php
require_once(dirname(__FILE__)."/wp-load.php");
query_posts('post_type=post&showposts=30');
get_header();?>
<div id="blog">
<?php
if (have_posts()) :
   while (have_posts()) :
      the_post();
         the_title();
         echo '<br />';
   endwhile;
else :
    echo 'Sorry, no posts found.';
endif;?>
</div>
<?php get_footer();

如果您将使用主题中的代码 使用与 Mr.Carlos 相同的代码,但没有 dir

require_once("/wp-load.php");