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

标签 php html css wordpress post

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

这是我的 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();
    ?>

我得到的是这样的

---header---

---html-content---

---list-of-posts---

---footer---

我尝试得到的是这样的

---header---

---html-content---

---list-of-posts---

---html-content---

---footer---

有没有办法在 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

关于php - 在 wordpress 的 html 内容中间添加帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170140/

相关文章:

html - iPad 显示的内容大于屏幕尺寸/视口(viewport)

html - 如何制作响应式 Bootstrap 下拉按钮?

添加类的 Javascript 仅在调整窗口大小后有效?

php - 从 Laravel 中的对象数组中获取值

php - 为什么返回对象而不是数组?

html - 如何使用 shell 脚本替换 html 注释

javascript - 尝试通过 PHP/Javascript/HTML 更改图像 onclick

css "position:fixed; overflow:auto;"错误的底部填充

php - 为什么不使用以下代码将值插入数据库?

php - 是否可以在 Laravel 查询生成器中按条件将自定义字段添加到 JSON?