【发布时间】:2014-07-28 16:04:33
【问题描述】:
css 已经被声明了,我所要做的就是添加一些 div 类。我的主题使用几乎空的 index.php,没有 html,但使用 get_template_part 函数并为帖子类型调用各种模板部分,因此它有几个 loop{template-name}.php 文件。我已经复制了一个用于博客索引并进行了修改的新循环。我的博客的索引调用了带有修改的适当模板 - hooray。但是我必须能够有条件地添加 div 类才能使转换完全工作。
由于博客的 index.php 遍历循环,我希望能够有条件地将一个 div 类添加到第一个返回的帖子,另一个 div 类到第二个,如果最后一个帖子是奇数,那么添加一个第三个 div 类。
这是我的 loop-template4.php 文件。
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div class="one"> /*I added this line here and it works for not found.
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'smpl' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found
for the requested archive. Perhaps searching will help find
a related post.', 'smpl' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
</div><!-- #post-0 -->
<?php endif; ?>
<?php
<?php while ( have_posts() ) : the_post(); ?>
/**
* I need to conditionally add one of 3 div classes here:
* odd `<div class="one_half">`,
* even `<div class="one_half last">`or
* odd and last, `<div class="one">`
*/
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="
<?php printf( esc_attr__(
'Permalink to %s', 'smpl' ), the_title_attribute( 'echo=0' ) );
?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
<?php do_action('skeleton_post_meta'); ?>
<?php if ( is_archive() || is_search() ) : // Only display excerpts for
archives and search. ?>
<div class="entry-summary clearfix">
<?php if (ot_get_option('show_post_thumbnails') && has_post_thumbn
nil()){
echo '<div class="alignleft">';
skeleton_thumbnailer('fourthree');
echo '</div>';
}?>
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php
if (ot_get_option('show_post_thumbnails') && has_post_thumbnail()) {
echo '<div class="alignleft">';
skeleton_thumbnailer('thumbnail');
echo '</div>';
}?>
<?php //the_content( __(
'Continue reading <span class="meta-nav">→</span>', 'smpl' ) );
do_action('skeleton_content');
?>
<div class="clear"></div>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' .
__( 'Pages:', 'smpl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
</div> <!-- conditional div class -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) {
do_action('skeleton_page_navi');
}?>
所以问题:
是否可能,因为循环尚未循环,我需要根据循环结果的数量有条件地添加 div 类?
【问题讨论】: