• Resolved tkse

    (@tkse)


    I’m coding my current webdesign from scratch, trying to optimize it while doing some design changes, and I was just wondering. Is there an easy way to style the most recent post on your site, without using a plugin or the pseudo-element in CSS?

    Currently I’m using :first-child to make the most recent post stand out, and I would rather not use a plugin, because of pagespeed. So any ideas, or should I just say screw you to those still out there without a browser supporting :first-child? ??

    I could always make the most recent post a featured post, but there’s gotta be an easier way to give the most recent post a special html-class without having to do that?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Currently I’m using :first-child to make the most recent post stand out, and I would rather not use a plugin, because of pagespeed.

    if you’re using :first-child, that shouldn’t require a plugin…
    The :first-child stuff should be in your CSS.

    say screw you to those still out there without a browser supporting :first-child? ??

    :first child support should be fairly good now, no? I never really thought about it, but I use it all the time. If only the folks on IE 7 can’t see this, then I would say, yes, it’s fine to ignore them.

    I just found this code that displays a thumbnail on the first post only – https://wordpress.stackexchange.com/questions/13020/display-thumbnail-only-on-the-very-first-post-in-the-loop
    Perhaps something like this would work for you too, but you could just add a class instead of display the thumbnail?

    Thread Starter tkse

    (@tkse)

    Oh, sorry. I didn’t mean to say I would need a plugin for it, I meant I rather not use either a plugin nor :first-child.

    I looked at the link, but couldn’t really see where to do something like that. Now, obviously my understanding of PHP isn’t that good. Did you have any specific code in mind?

    Thanks for the answer btw. ??

    as you are coding the theme yourself, make sure for instance to have post_class() in the wrapping div for each post;

    https://codex.www.ads-software.com/Function_Reference/post_class

    you can then add a filter into functions.php of your theme, to add a special css class for the first post in the loop;

    example:

    add_filter( 'post_class', 'special_first_post_class' );
    
    function special_first_post_class( $classes ) {
      if( !is_singular() ) {
        global $wp_query;
        if( $wp_query->current_post == 0 ) $classes[] = 'first-post';
      }
      return $classes;
    }
    Thread Starter tkse

    (@tkse)

    Absolutely brilliant! Yeah, I run my theme of twentytwelve, so post_class() was already included in the article-tag.

    Anyways, thank you! Should be much easier to code as well now, as I don’t have to struggle with :nth-child on some of the other pages. ??

    Thread Starter tkse

    (@tkse)

    Solved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Different styling for first post’ is closed to new replies.