dongyong1897 2017-06-27 13:58
浏览 14

Wordpress - 如果第一篇帖子添加了所选的课程

Somehow in my custom posts-blog-page in wordpress I want to be able to add the class "selected" on the first-post.

What I am doing is the following:

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

                    <?php if ($postCount == 0) { ?>
                        <li class="selected" data-date="<?php the_time('F jS, Y'); ?>">
                            <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                                <?php the_post_thumbnail(); ?>
                                <div class="news-inner">
                                    <div class="news-inner-wrapper">
                                        <h4><?php the_title(); ?></h4>
                                        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                                        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                                    </div>

                                </div>
                            </a>
                        </li>
                    <?php  } else { ?>
                        <li data-date="<?php the_time('F jS, Y'); ?>">
                            <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                                <?php the_post_thumbnail(); ?>
                                <div class="news-inner">
                                    <div class="news-inner-wrapper">
                                        <h4><?php the_title(); ?></h4>
                                        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                                        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                                    </div>
                                </div>
                            </a>
                        </li>
                    <?php } ?>
                    <?php endwhile; ?>
                    <?php endif; ?>

However this applies the class "selected" on all li's.

Any ideas?

  • 写回答

2条回答 默认 最新

  • duanjiao5261 2017-06-27 14:03
    关注

    There's a lot of duplication there, just to display the class if $postcount == 0
    Also, it shouldn't evaluate to true, since you $postcount++ too early.

    Try this:

    <?php if (have_posts()) : ?>
        <?php $postcount = 0; ?>
        <?php while (have_posts()) : the_post(); ?>
    
            <li class="class="<?php if($postcount == 0) { echo 'selected'; } ?>" data-date="<?php the_time('F jS, Y'); ?>">
                <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                    <?php the_post_thumbnail(); ?>
                    <div class="news-inner">
                        <div class="news-inner-wrapper">
                            <h4><?php the_title(); ?></h4>
                            <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                            <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                        </div>
    
                    </div>
                </a>
            </li>
    
            <?php $postcount++; ?>
    
        <?php endwhile; ?>
    <?php endif; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?