我在Wordpress中使用bones样板主题,在主页上显示了一个包含Wordpress中最近三篇帖子的三列的部分。
bones主题使用响应性960网格,在任何网格使用中,我们都需要添加first
到第一个元素和last
到最后一个元素。我已经尝试从this answer 在他们的支持论坛上。
以下是当前使用的代码:-
<section class="home-section clearfix" id="what-we-say">
<div class="wrap">
<hgroup class=""><h2>What We Say</h2></hgroup>
<div id="blog-posts">
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query(\'showposts=3\' . \'&paged=\'.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $counter = 1; //counter tracks the number of the post we\'re on ?>
<?php // This code uses the modulus operator to check the remainder of the $counter value in the current iteration and adds the \'first\' or \'last\' class based on either number sequence ?>
<li class="blog-post fourcol <?php if ($counter % 3 == 0) { echo \'last\';} elseif ($counter % 3 == 1) { echo \'first\';} ?>">
<article id="post-<?php the_ID(); ?>" <?php post_class( \'clearfix\' ); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<header class="article-header">
<h3><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h3>
<p class="byline vcard"><?php printf( __( \'Posted <time class="updated" datetime="%1$s" pubdate>%2$s</time> by <span class="author">%3$s</span>.\', \'bonestheme\' ), get_the_time( \'Y-m-j\' ), get_the_time( __( \'F jS, Y\', \'bonestheme\' ) ), bones_get_the_author_posts_link() ); ?></p>
</header>
<section class="entry-content clearfix" itemprop="articleBody">
<?php the_excerpt(); ?>
</section>
<footer class="article-footer">
<p class="clearfix"><?php the_tags( \'<span class="tags">\' . __( \'Tags:\', \'bonestheme\' ) . \'</span> \', \', \', \'\' ); ?></p>
</footer>
</li>
<?php $counter++; // This increases the value of $counter by 1 for every loop iteration ?>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link(\'« Previous Posts\'); ?></div>
<div class="next"><?php previous_posts_link(\'Newer Posts »\'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link(\'« Previous Posts\'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
</section>
然而,这是在添加
first
到每个而不是
first
在第一(1)列和
last
在最后(第3)列。
有人能从上面看出为什么我的代码不能按预期工作吗?提前谢谢。
最合适的回答,由SO网友:zigojacko 整理而成
我刚刚设法解决了这个问题。
这
<?php $counter = 1; //counter tracks the number of the post we\'re on ?>
需要高于
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query(\'showposts=3\' . \'&paged=\'.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
(不低于)。
正常工作的结束代码是:-
<?php $counter = 1; //counter tracks the number of the post we\'re on ?>
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query(\'showposts=3\' . \'&paged=\'.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
问题中发布的片段的其余部分保持不变。