wordpress循环通过自定义php页面中的帖子

问题描述 投票:0回答:4

我试图在自定义的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();?>
php wordpress posts custom-pages
4个回答
1
投票

您应该通过此文件的完整路径要求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_Post类的成员变量。去https://codex.wordpress.org/Class_Reference/WP_Post参考。

在这里,您重新编写了代码,显示了已发布的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();

0
投票

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

你试图回应一个以致命错误结束的对象。此外,如果您想查看所有帖子,则the_post不正确。在函数参考中查找:https://codex.wordpress.org/Function_Reference/the_post。我会做其他(google smth喜欢“获取所有帖子”)。


0
投票

正如Mr.Carlos通过评论建议我将参数传递给have_posts方法,现在它可以工作了!这段代码对我有用

`require('./wp-blog-header.php');
 get_header();?>
 <div id="blog">
 <?php if(have_posts(array('post_type' =>'page'))
   {
    echo"anything";
   }?>
 </div>
 <?php get_footer();?>`

0
投票

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

require_once("/wp-load.php");
© www.soinside.com 2019 - 2024. All rights reserved.