dse3168 2015-08-29 20:15
浏览 52

Wordpress循环无法正常工作

I'm working on creating my own WP template so I'm adjusting my pure HTML + CSS to work properly with WP. However, I have some issues with creating the separate blog page, which is not the main page. This is the result I want WP loop to generate for me:

<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>

So this is what my PHP code looks like in the page template:

<? $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; ?>

From what I know, this must work just right, showing 4 posts I already have put in the admin panel. But it doesn't work, the code that is being generated looks like this:

    <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>

What's my problem and how do I make it work? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dreamer1231 2015-08-29 22:53
    关注

    There are errors on line 1 and 4. <? should be <?php.

    But there is no need to open and close php every time.

    <?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; ?>
    

    I would suggest to use a code editor like Notepad++ to write your code. This makes it a whole lot easier to see errors.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统