Search code examples
wordpressfor-loopcustom-post-type

Why can't the content be output in wordpress custom post type?


I thought the content would be printed on this code. However, this code normally outputs title, DATE value, but not content value.

I've tried the_content(); There was no difference. What is the problem?

HERE is my Full code. I'd be grateful if someone could help.

            <?php
            $peoplePosts = get_posts(
               array(
                   'post_type'      =>  'notice',
                   'posts_per_page'  =>  -1
               ));
            if (is_array($peoplePosts)) {
                foreach ( $peoplePosts as $peoplePost) {
                    $get_tit = get_the_title($peoplePost);
                    $get_cont = get_the_content($peoplePost);
            ?>

                <li class="i-item i-item--active">
                    <a href="#" class="i-link">
                        <div class="i-subject"><?php echo $get_tit; ?></div>
                        <div class="i-date i-date--notice"><?php echo get_the_date('Y-m-d'); ?></div>
                    </a>
                    <div class="i-qnaboxs">
                        <div class="notice_box"><?php echo $get_cont; ?></div>
                    </div>
                </li>
            <?php           
                    }
                }
            ?>

regard.


Solution

  • In this part:

    foreach ( $peoplePosts as $peoplePost)
    
    1. Rename the variable to $post.

    2. Call setup_postdata().

    3. Then you can call the_content(), etc.

    So:

    foreach ( $peoplePosts as $post) {
      setup_postdata( $post );
      ... your code here
    }