wordpress循环自定义php页面

时间:2016-02-16 21:31:25

标签: php wordpress posts custom-pages

我试图在自定义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();?>

4 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

你试图回应一个以致命错误告终的对象。此外,如果您想查看所有帖子,则the_post不正确。在函数引用中查找它:https://codex.wordpress.org/Function_Reference/the_post。我会做其他的(google smth like&#34;得到所有帖子&#34;)。

答案 2 :(得分: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();?>`

答案 3 :(得分:0)

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

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