dousuie2222 2019-04-16 20:22
浏览 120

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

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.

I have tried using php functions to grab the content but they don't seem to be working.

<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>

I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dongqu9972 2019-04-16 20:31
    关注

    It sounds like you may be trying to retrieve the post content from outside the loop.

    If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post 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.
        ?>
    

    E.g. you should just be able to do:

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

    Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行