自定义主页上的第一篇文章wordpress

自定义主页上的第一篇文章wordpress,wordpress,custom-post-type,Wordpress,Custom Post Type,我需要修改这段代码,以便第一篇文章显示“first”类,同时也显示firsttop类和firstbottom类。其余的内页(如第/2/页、第/3/页等)将没有此类。我试过很多东西,请不要确定这里出了什么问题 <?php if (have_posts()) : $count = 0; while (have_posts()): the_post(); if (get_post_type() == 'post'): ?> <?php if (!is_single() &a

我需要修改这段代码,以便第一篇文章显示“first”类,同时也显示firsttop类和firstbottom类。其余的内页(如第/2/页、第/3/页等)将没有此类。我试过很多东西,请不要确定这里出了什么问题

    <?php
if (have_posts()) :
$count = 0;
while (have_posts()):
the_post();
if (get_post_type() == 'post'):
?>

<?php
if (!is_single() && $count == 0):
?>
<div class="firsttop<?php echo !is_home() ? "notop" : "" ?>"></div>
<?php endif; ?>

<article class="post <?php echo !is_single() ? "preview" : "" ?> <?php echo $count == 0 ? "first" : "" ?> <?php echo !is_home() ? "full" : "" ?>">
    <p class="byline">
        <?php the_time('F j, Y'); ?>
    </p>
    <h3>
        <a href="<?php echo get_permalink() ?>"><?php the_title(); ?> </a>
    </h3>
<div>
        <?php the_content('<span class="more">Read&nbsp;More...</span>'); ?>
</div>
    <?php include ('post-info.php'); ?>
</article>

<?php
if (!is_single() && $count == 0):
?>
<div class="firstbottom<?php echo !is_home() ? "nobottom" : "" ?>"></div>
<?php endif; ?>

<?php
endif;
$count++;
endwhile;
endif;
?>


你有没有试过用过滤器来过滤

阅读更多关于wordpress添加过滤器的信息

您可以创建一个新插件,或者将其添加到主题目录中的functions.php文件中

这家伙是用post_类做的,但是如果你没有在posts循环页面中使用post_类,它可能对你不起作用

以下是他的代码片段:

add_filter( 'post_class', 'wps_first_post_class' );
function wps_first_post_class( $classes ) {
    global $wp_query;
    if( 0 == $wp_query->current_post )
        $classes[] = 'first';
        return $classes;
}