我试图在主页内容中间添加帖子
这是我的 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(']]>', ']]>', $content);
echo $content;
如果您不希望该帖子与您的常规帖子混为一谈,请创建一个特殊的帖子类型。你可以使用 CPT ui
关于php - 在 wordpress 的 html 内容中间添加帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170140/