简体   繁体   中英

Wordpress - If first post add the class selected

Somehow in my custom posts-blog-page in wordpress I want to be able to add the class "selected" on the first-post.

What I am doing is the following:

                    <?php if (have_posts()) : ?>
                    <?php $postcount = 0; ?>
                    <?php while (have_posts()) : the_post(); ?>
                    <?php $postcount++; ?>

                    <?php if ($postCount == 0) { ?>
                        <li class="selected" data-date="<?php the_time('F jS, Y'); ?>">
                            <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                                <?php the_post_thumbnail(); ?>
                                <div class="news-inner">
                                    <div class="news-inner-wrapper">
                                        <h4><?php the_title(); ?></h4>
                                        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                                        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                                    </div>

                                </div>
                            </a>
                        </li>
                    <?php  } else { ?>
                        <li data-date="<?php the_time('F jS, Y'); ?>">
                            <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                                <?php the_post_thumbnail(); ?>
                                <div class="news-inner">
                                    <div class="news-inner-wrapper">
                                        <h4><?php the_title(); ?></h4>
                                        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                                        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                                    </div>
                                </div>
                            </a>
                        </li>
                    <?php } ?>
                    <?php endwhile; ?>
                    <?php endif; ?>

However this applies the class "selected" on all li's.

Any ideas?

There's a lot of duplication there, just to display the class if $postcount == 0
Also, it shouldn't evaluate to true, since you $postcount++ too early.

Try this:

<?php if (have_posts()) : ?>
    <?php $postcount = 0; ?>
    <?php while (have_posts()) : the_post(); ?>

        <li class="class="<?php if($postcount == 0) { echo 'selected'; } ?>" data-date="<?php the_time('F jS, Y'); ?>">
            <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
                <?php the_post_thumbnail(); ?>
                <div class="news-inner">
                    <div class="news-inner-wrapper">
                        <h4><?php the_title(); ?></h4>
                        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
                        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
                    </div>

                </div>
            </a>
        </li>

        <?php $postcount++; ?>

    <?php endwhile; ?>
<?php endif; ?>

Theres useless code there. Try this

<?php if (have_posts()) {
    $postcount = 0;
    while (have_posts()) : the_post();
        if ($postcount == 0) { ?>
             <li class="selected" data-date="<?php the_time('F jS, Y'); ?>">
        <?php } else { ?>
             <li data-date="<?php the_time('F jS, Y'); ?>">
        <?php } ?>
        <a class="news-box" href="<?php the_permalink(); ?>" target="_self">
        <?php the_post_thumbnail(); ?>
        <div class="news-inner">
        <div class="news-inner-wrapper">
        <h4><?php the_title(); ?></h4>
        <div class="read-more"><?php the_excerpt(__('Continue reading »','example')); ?></div>
        <div class="news-inner-article-date"><small>By <?php the_author_link(); ?></small></div>
        </div>

    </div>
    </a>
    </li>
    <?php 
      $postcount++;
      endwhile; 
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM