如何使用自定义帖子模板显示博客帖子的内容?

时间:2019-04-16 20:22:40

标签: php html wordpress

我正在使用自己的自定义WordPress主题,却无法显示博客文章的内容。我可以显示标题和使用php发布的日期,但无法在页面上显示任何段落,图像,标题等。我在博客文章的内容中使用了Gutenberg块(默认)。

我尝试使用php函数来获取内容,但是它们似乎无法正常工作。

<div class="col-md-6 col-md-offset-3">

        <p class="date"><span class="glyphicon glyphicon-time"> 
         </span> <?php echo get_the_date();?></p><br />

        <p><?php $content = apply_filters('the_content', $post- 
          >post_content);?></p>

</div>

我希望该帖子的内容显示在div容器中,但该功能未获取该内容。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

听起来您可能正在尝试从循环外部检索帖子内容。

如果您查看主题的帖子模板,例如2017,正是这一点发挥了魔力。甚至没有必要传递帖子ID:

    <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'components/page/content', 'page' );
            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;
        endwhile; // End of the loop.
    ?>

例如您应该可以:

<?php
        while ( have_posts() ) : the_post();
            the_content();
        endwhile; // End of the loop.
?>

从上面链接上的代码开始,或者复制您正在使用的主题的single.php文件并将其用作自定义帖子页的基础,是一个好主意吗?

相关问题