Wordpress循环无法正常工作

时间:2015-08-29 20:15:32

标签: php html css wordpress loops

我正在创建自己的WP模板,所以我调整纯HTML + CSS以便与WP一起正常工作。但是,我在创建单独的博客页面时遇到了一些问题,这不是主页面。这是我希望WP循环为我生成的结果:

<div class="news-body news-body1">
<div class="article-header">Title</div>
<div class="article-body"><img src="images/article1.jpg" alt="article1" id="text-image">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam suscipit placerat diam, ac sagittis felis ornare eget.</p>
</div>
<div class="article-readmore"><a href="#">Read more</a></div>

这就是我的PHP代码在页面模板中的样子:

<? $postCount = 0; ?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <?if (++$postCount == 1) { ?>
            <div class="news-body news-body1">
        <?php } else { ?>
            <div class="news-body">
        <?php } ?>
            <div class="article-header"><?php the_title(); ?></div>
            <div class="article-body"><?php the_content(); ?></div>
            <div class="article-readmore"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div>
    <?php endwhile; ?>
<?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center"><?php _e("Sorry, but you're looking for something else"); ?></p>
<?php endif; ?>

据我所知,这必须正常工作,显示我已经放入管理面板的4个帖子。 但它不起作用,生成的代码如下所示:

    <div class="news-body news-body1">
                        <div class="article-header">News</div>
            <div class="article-body"></div>
            <div class="article-readmore"><a href="http://lol.ru/news">Read More</a></div>
        </div>
        </div>

我的问题是什么?如何让它发挥作用? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

第1行和第4行有错误。<?应为<?php

但是没有必要每次都打开和关闭php。

<?php $postCount = 0;
if (have_posts()) :
    while (have_posts()) : the_post();
    if (++$postCount == 1) { ?>

<div class="news-body news-body1">

<?php } else { ?>

<div class="news-body">

<?php } ?>

    <div class="article-header"><?php the_title(); ?></div>
    <div class="article-body"><?php the_content(); ?></div>
    <div class="article-readmore"><a href="<?php the_permalink(); ?>">Read More</a></div>
</div>

<?php endwhile;
else : ?>

<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you're looking for something else"); ?></p>

<?php endif; ?>

我建议使用像Notepad++这样的代码编辑器来编写代码。这使得查看错误变得更容易。