繁体   English   中英

在WordPress的HTML内容中间添加帖子

[英]add posts in middle of html content in wordpress

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

这是我的front-page.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-content ---

--- 帖子列表 -

---页脚---

我试图得到的是这样的

-标题-

--- html-content ---

--- 帖子列表 -

--- html-content ---

---页脚---

有没有一种方法可以获取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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM