【问题标题】:WordPress - how to get posts content and titleWordPress - 如何获取帖子内容和标题
【发布时间】:2018-05-20 08:40:53
【问题描述】:

我是 WordPress 新手,我需要检索所有帖子的内容和标题,但找不到我的方式。这是我的代码。

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

$the_query = new WP_Query( ['posts_per_page' = -1]);

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $a= $the_query->post_content;
        echo $a;

    }

    wp_reset_postdata();
}

它给了我一个空白页

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    一旦循环存储了帖子,您就可以使用其他方法来获取有关帖子的信息。

    例如一张卡片,其中包含一张图片、一段内容的标题等。

    <section>
       <div class="container">
          <div class="card">
             <div class="row">
                <div class="col-md-6">
                   <a href='<?php the_permalink();?>'>
                      <div class="card-img-bottom" style=' height:100%;background-image: url(<?php the_post_thumbnail_url( 'full' ); ?>);background-size:cover;'>
                      </div>
                   </a>
                </div>
                <div class="col-md-6">
                   <div class="card-block">
                      <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                      <p class="card-text"><?php  the_excerpt(); ?></p>
                      <a href="<?php the_permalink(); ?>" class="btn btn-primary">Read More</a>
                   </div>
                </div>
             </div>
          </div>
       </div>
    </section>
    

    循环返回的每个帖子都将嵌入到该上下文中(HTML/CSS)

    在页脚调用上方的页面底部,您还可以像这样结束循环:

     <?php endwhile; else : ?>
      <?php endif; ?>
    

    还要指定。您可以通过

    获取内容
     the_content();
    

    标题与

    the_title();
    

    以及帖子的链接

     the_permalink();
    

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多