在我的自定义帖子 - wordpress中的blog-page中,我希望能够在首帖上添加“selected”类。
我正在做的是以下内容:
<?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; ?>
然而,这适用于所有li的“选定”类。
有什么想法吗?
答案 0 :(得分:-1)
那里有很多重复,只是为了显示班级$postcount == 0
此外,它不应该评估为真,因为你$postcount++
太早了。
试试这个:
<?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; ?>
答案 1 :(得分:-1)
那里有无用的代码。试试这个
{{1}}