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

时间:2013-06-21 20:59:56

标签: wordpress custom-post-type

我需要更改此代码,以便第一个帖子显示“first”类,并且还有firsttop类和firstbottom。内页的其余部分,例如page / 2 /,page / 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;
?>

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用过滤器?

在此处详细了解wordpress add_filter http://codex.wordpress.org/Function_Reference/add_filter

您可以制作新插件或将其添加到主题目录中的functions.php文件中。

这个人用post_class做了,但如果你没有在你的帖子循环页面中使用post_class,它可能不适合你。 http://wpsnipp.com/index.php/functions-php/add-class-to-first-post-in-the-loop/

以下是他的代码片段:

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;
}