首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将类添加到wordpress循环中

将类添加到wordpress循环中
EN

Stack Overflow用户
提问于 2014-05-17 23:21:07
回答 2查看 2.1K关注 0票数 0

我想在前四篇文章中增加一门课。要在html上这样打印:

代码语言:javascript
复制
 <post id="post-1"  class="classes first">
 <post id="post-2" class="classes second">
 <post id="post-3" class="classes third">
 <post id="post-4" class="classes fourth">

我的圈套:

代码语言:javascript
复制
<?php query_posts('cat=15'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>

非常感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-17 23:33:05

你可以这样做:

代码语言:javascript
复制
<?php query_posts('cat=15'); ?>
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="post<?php echo $count; ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div>
<?php $count++; ?>
<?php endwhile; endif; ?>

在css中,使用div.post1、div.post2、div.post3和div.post4

票数 3
EN

Stack Overflow用户

发布于 2014-05-18 02:02:51

如果使用的是班级函数,则可以使用post_class筛选器。

代码语言:javascript
复制
add_filter( 'post_class', 'add_class_to_first_four' );

function add_class_to_first_four( $classes ) {

    global $wp_query;

    $cur = $wp_query->current_post;

    if ( 0 == $cur ) $classes[] = 'first';

    if ( 1 == $cur ) $classes[] = 'second';

    if ( 2 == $cur ) $classes[] = 'third';

    if ( 3 == $cur ) $classes[] = 'fourth';

    return $classes;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23717060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档