dpd20130 2016-02-16 21:31
浏览 61
已采纳

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

I trying to loop through posts in a custom php page but no matter what I do, no posts are found here is the code I wrote in 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条回答 默认 最新

  • doudiza9154 2016-02-17 01:21
    关注

    You should require wp-load.php via the full path to this file.

    Hardcoded example:

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

    Softcoded example (suposing your file is in the same directory as WordPress):

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

    You have also to query the posts before you display them. So, you need to add this line to your code:

    query_posts('post_type=post');
    

    The query arguments may vary depending on what you want to display. Some of them are the member variables of the WP_Post class. Go to https://codex.wordpress.org/Class_Reference/WP_Post for reference.

    Here you have a re-writing of your code that displays the titles of the 30 latest posts published:

    <?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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?