希望我的page.php文件显示不同的页面类别循环


Want My page.php file to display different pages categories Loops

在我的page.php文件中,我的目标是使用以下条件为不同的页面设置不同类别的循环:

if (is_page(array('pagename1', 'pagename2'))) {
}

但当我把下面的循环放在"is_page"大括号内时,我得到了空白页

<?php
    $newsposts = get_posts('cat=4');
    foreach($newsposts as $post) :
        setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>">
    <div class="col-lg-3 col-md-4 col-sm-6">
        <div class="newsbox2 newsboxcolor1">
            <div class="newsbox3pix boxeq">
                <?php
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail( 'full', array() );
                    }else { 
                 ?>
                     <img src="<?php echo get_bloginfo('template_directory');?>/images/noimage.jpg" alt="no image">
                <?php } ?>
            </div>
            <div class="newstitle boxeq2">
                <span class="matchtime"><?php the_time();?><br></span>
                <h3><?php the_title(); ?></h3>
            </div>
            <span class="fa fa-comments cmnt"> <?php comments_number('0','1','%'); ?></span>
     </div><!--newsbox2-->
  </div>
        </a>
<?php endforeach; ?>

如何集成这些,以便将所有页面条件都放在一个文件中?如果有任何建议,我将不胜感激。干杯

类别不用于页面,而是用于帖子。

代码get_posts('cat=4')将获取该类别下的所有帖子。

如果您将在page.php中编写此代码,那么在从后端创建的所有页面中,您将获得指定类别的帖子。