Php Wordpress循环帖子的条件类
css已经声明了,我所要做的就是添加一些div类。我的主题使用一个几乎为空的index.php,没有html,但使用get_template_part函数并为post类型调用各种模板部分,因此它有几个循环{template name}.php文件。我复制了一个博客索引,并做了一个新的循环修改。我的博客的索引调用经过修改的适当模板-hooray。但是,我必须能够有条件地添加div类,以使转换完全工作 由于blog的index.php在循环中运行,我希望能够有条件地将一个div类添加到返回的第一篇文章中,将另一个div类添加到第二篇文章中,如果最后一篇文章是奇数的,则添加第三个div类 这是我的loop-template4.php文件Php Wordpress循环帖子的条件类,php,html,css,wordpress,Php,Html,Css,Wordpress,css已经声明了,我所要做的就是添加一些div类。我的主题使用一个几乎为空的index.php,没有html,但使用get_template_part函数并为post类型调用各种模板部分,因此它有几个循环{template name}.php文件。我复制了一个博客索引,并做了一个新的循环修改。我的博客的索引调用经过修改的适当模板-hooray。但是,我必须能够有条件地添加div类,以使转换完全工作 由于blog的index.php在循环中运行,我希望能够有条件地将一个div类添加到返回的第一篇文
<?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');
}?>
/*我在这里添加了这一行,它适用于not found。
/**
*我需要在这里有条件地添加3个div类中的一个:
*奇怪的``,
*甚至``或者
*奇怪的和最后的,`
*/
据我所知,无法知道循环中的最后一篇文章何时显示,因此首先我们需要在“while(have_posts()):the post()行之前添加一些代码。对于上下文,我已将您的整个代码块复制到此处,并添加了注释以显示新代码的位置:
<?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
/**
* BEGIN: Additional code to add conditional classes
*/
// How many posts are going to be displayed? Let's count here:
$total = 0;
while ( have_posts() ) : the_post();
$total++;
endwhile;
// Reset the loop to actually start displaying them
rewind_posts();
// Add this line before your loop to prevent notices being thrown
$count = 1;
while ( have_posts() ) : the_post(); ?>
<?php
// Add some conditions to determine the class
if ($count % 2) {
// If the post displayed is an odd number
$class = 'one_half last';
} else {
// If the post displayed is an even number
$class = 'one_half';
}
if ($count == $total && ($count % 2)) {
// If the post is the last post, and it is odd
$class = 'one';
}
// Finally, increment the count
$count++;
?>
<?php // If you need an extra div, just put it in like so... ?>
<div class="<?php echo $class; ?>">
<?php
// If you need the existing div to simply have the class added,
// Then we pass it into the post_class() function
?>
<div id="post-<?php the_ID(); ?>" <?php post_class($class); ?>>
<h2 class="entry-title">
/*我在这里添加了这一行,它适用于not found。
非常感谢你的帮助。我有一个完全可以工作的模板,可以将代码添加到我希望它出现的地方。再次感谢你让这一切顺利进行。@Asa-我的荣幸。很高兴我能帮忙。