在wordpress的html内容中间添加帖子


add posts in middle of html content in wordpress

我试图在主页内容中间添加帖子

这是我的前台代码.php

<?php get_header(); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
    <?php endwhile; else: ?>
        <p><?php _e('Sorry, this page does not exist.'); ?></p>
    <?php endif;
        $allPosts = new WP_Query('cat=5&posts_per_page=2');
        if ($allPosts -> have_posts()):
            while($allPosts -> have_posts()) : $allPosts -> the_post();
    ?>      <div class="container">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
                <hr>
            </div>
    <?php
            endwhile;
        else:
        endif;
        wp_reset_postdata();
    ?>

我得到的是像这个

---标题---

---html内容---

---帖子列表---

---页脚---

我试着得到的是像这个

---标题---

---html内容---

---帖子列表---

---html内容---

---页脚---

有没有一种方法可以在html内容中间获取帖子列表?

你应该把你想要的内容放在一个单独的帖子中的帖子列表下,并根据帖子列表下的ID打印出来。

类似于:

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;

如果你不想让帖子和你的常规帖子在一起,那就创建一个特殊的帖子类型。您可以使用CPT ui。