在wordpress

时间:2015-12-09 03:13:43

标签: 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();
    ?>

我得到的是这样的

  

--- ---头

     

--- HTML内容---

     

---的列表的根柱子 ---

     

--- ---页脚

我试图得到的是这样的

  

--- ---头

     

--- HTML内容---

     

---的列表的根柱子 ---

     

--- HTML内容---

     

--- ---页脚

有没有办法获取html内容中间的帖子列表?

1 个答案:

答案 0 :(得分:0)

您应该将您想要的内容放在帖子列表中的单独帖子中,然后通过帖子列表下的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