开发者

Picking the most recent post in WordPress with php

开发者 https://www.devze.com 2023-02-11 23:06 出处:网络
I am trying to do an if/else-statement on my WP-template. But i can\'t seem to pick the right element.

I am trying to do an if/else-statement on my WP-template. But i can't seem to pick the right element.

I wan't it to look for the latest post on the blog. I already added a script that add a class of .first to the latest post.

What im trying to do in dummy-php-language:

<?php
if ( class is .first) { do something to .first}
else () { do something to everything else }
?>

i've looked in the wp-documentation. But i can't seem to find anything that will select "开发者_C百科latest post"...

Thanks

Jonas


A very simple solution would be to keep a count of the post you're at, and do something if it's the first. In the index.php, in the have_posts() loop:

<?php if (have_posts()) : $count = 0; ?>
    <?php while (have_posts()) : the_post(); $count++; ?>
        <?php if($count ==1) ?>
            // do something to first
        <?php else ?>
            // do something to everything else
        <?php endif; ?>              
    <?php endwhile; else: ?>
<?php endif; ?>  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号