Wordpress从个人布局的类别中获取帖子


Wordpress getting posts from category in personal layout

我正试图从evenementen获得帖子。我只想要4个职位,因为我的布局是一个2x2列。本专栏为顶部和底部提供了一个特殊模板。我想在这一列中显示4个元素,但是如果少于4个元素,我不想改变布局。

在PHP文件中,我循环evenementen中的所有帖子,并希望在特定列中添加这些帖子。

PHP文件

<?php 
                $args = array( 'posts_per_page' => 4, 'offset'=> 1, 'category' => 0 );
                $myposts = get_posts( $args );
                $count = 0;
                foreach ( $myposts as $post ) : setup_postdata( $post ); 
                $count++;
                    if ($count == 1) {
                            $title1 = the_title();
                            $date1 = the_date();
                            $link1 = the_permalink();
                        }
                    elseif ($count == 2) {
                            $title2 = the_title();
                            $date2 = the_date();
                            $link2 = the_permalink();
                        }
                    elseif ($count == 3) {
                            $title3 = the_title();
                            $date3 = the_date();
                            $link3 = the_permalink();
                        }
                    elseif ($count == 4) {
                            $title4 = the_title();
                            $date4 = the_date();
                            $link4 = the_permalink();
                        }
                    else {
                        }           
                endforeach;
                ?>
                <div class="wrapper">
                    <div id="bigone">
                        <div class="wrapper">
                            <h4 class="push"><?php echo $title1; ?></h4>
                            <div id="one"> <p class="greytext"><?php echo $date1; ?></p></div>
                            <div id="two"> <p class="opmaak"><a href="<?php echo $link1; ?>">Evenementen</a></p></div>
                        </div>
                    </div>
                    <div id="bigtwo">
                    <div class="evenementenborder">
                            <div class="wrapper">
                                <h4 class="push"><?php echo $title2; ?></h4>
                                <div id="one"> <p><?php echo $date2; ?></p> </div>
                                <div id="two"> <p class="opmaak"><a href="<?php echo $link2; ?>">Evenementen</a> </p></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="container">
            <div class="evenementenfooter">
                <div class="wrapper">
                    <div id="bigone">
                        <div class="wrapper">
                            <h4 class="push"><?php echo $title3; ?></h4>
                            <div id="one"> <p class="greytext"><?php echo $date3; ?></p></div>
                            <div id="two"> <p><a href="<?php echo $link3; ?>">Evenementen</a></p></div>
                        </div>
                    </div>
                    <div id="bigtwo">
                    <div class="evenementenborder">
                            <div class="wrapper">
                                <h4 class="push"><?php echo $title4; ?></h4>
                                <div id="one"> <p class="greytext"><?php echo $date4; ?></p> </div>
                                <div id="two"> <p><a href="<?php echo $link4; ?>">Evenementen</a> </p></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

这是我到目前为止最接近的。但这并没有把文本放在正确的位置,而且超链接也在文本中。

网页

我想问你们:解决这个问题的最好方法是什么?或者如果我在代码中犯了错误为什么它不在正确的位置上?

我发现信息是直接放在页面上的,echo找不到信息,因为当时它是空的。我不知道怎么解。如果你看到我做错了什么,请发帖。

我认为这是缓冲,放在开始

ob_start();

在文档的末尾

ob_end_flush();

再试一次。查看这里关于缓冲的文档