对于Wordpress中的每个循环:内容不返回

时间:2017-04-04 16:22:35

标签: php wordpress

我查询了wordpress循环并尝试在foreach循环中返回post数据,这样我就可以获得第一个项目并以不同的方式设置样式。但是,数据返回foreach循环中的帖子ID。但是我似乎无法获得内容。有人可以帮忙吗?

<div class="tab-content">
<?php  
        $args = array(  'child_of' => $post->ID,  'parent ' => $post->ID, 'hierarchical' => 0, 
        'sort_column' => 'menu_order', 'sort_order' => 'asc' );

        $mypages = get_pages( $args );

        $first = true; 

        foreach( $mypages as $page ) {

            $content = $page->post_content;
            $content = apply_filters( 'the_content', $content );

              if ( $first )
              {
                // do something
                $first = false; //in order not to get into the if statement for the next loops 
                ?>
                    <div id="<?php echo $page->ID; ?>" class="tab-pane fade in active">
                     <?php echo $content; ?>
                   </div>
              <?php
              }
              else
              {
                // do something else for all loops except the first
                ?>
                   <div id="<?php echo $page->ID; ?>" class="tab-pane fade">
                     <?php echo $content; ?>
                   </div>
               <?php  
              }
        }

?>
</div>

3 个答案:

答案 0 :(得分:1)

$content = $page->post_content;

事实上应该是:

$content = $post->post_content;

实际上,将其分配给变量$content并不能真正获得任何好处。您可以改为使用echo $post->post_content;

答案 1 :(得分:0)

您的$ content = $ page-&gt; post_content;应该说$ content = $ post - &gt; post_content

答案 2 :(得分:0)

Thanks for all the help. Sorry I was having a server error which was cleared. So the query worked fine.