• I’m fiddling around with a new project and got the brilliant idea to style some post differently. This is something I’ve seen others do, at https://www.binarybonsai.com for instance, so is there a plugin or nice CSS/theme thing to do here?

    (I want some posts to be more visible than others – larger fonts, other background and such.)

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • There are probably several different ways to do this. I’m not aware of a plugin, but I think it would just be easier to figure out how you want to identify the posts as being unique and then assign them a class and style that class differently in your style.css file.

    For example, if you wanted to style the posts by category you could do the following:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>



    <div class="
    <?php
    foreach((get_the_category()) as $cat)
    {
    echo $cat->category_nicename;
    }
    ?>">


    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    This spits out the post’s category slug as a class for each post’s div. Having done that, then you can just set up your style sheet to style accordingly.

    You can read more about get_the_category() here:
    https://codex.www.ads-software.com/Template_Tags/get_the_category

    I don’t have a way to test this to see if it actually worked, so let me know.

    I forgot to mention, you’ll probably want to put a space after the category slugs when you’re echoing those. That way if you have a post assigned to three categories the output will be

    <div class="category1 category2 category3">

    and not

    <div class="category1category2category3">

    To do this change the line in the above post where it reads

    echo $cat->category_nicename;

    to


    echo $cat->category_nicename . ' ';

    It worked like a charm, thanks! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Styling some posts differently’ is closed to new replies.