How to add a course to latest posts on WordPress home page?
P粉073857911
P粉073857911 2024-03-21 23:09:34
0
1
314

I am using the following function in WordPress to display the latest three posts on the homepage. Is it possible to add the class class="latest-posts-right" to the last div so that I can give it another style? How can I do this?

<?php
/* Show latest posts on homepage */
function latest_post() {

    $args = array(
        'posts_per_page' => 3, /* how many post you need to display */
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post', /* your post type name */
        'post_status' => 'publish'
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            ?>
            <div class="latest-posts">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <p><?php the_excerpt(); ?></p>
            <div class="readmore"><a href="<?php the_permalink(); ?>">Read more »</a></div>
            </div>
            <?php
        endwhile;
    endif;
}

add_shortcode('lastest-post', 'latest_post');
?>

P粉073857911
P粉073857911

reply all(1)
P粉384366923

CSS selector: last-child can work for you

div.latest-posts:last-child{
   /* css rules */
}

or

div.latest-posts:nth-child(3){
   /* css rules */
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!