WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Styling some posts differently (4 posts)

  1. Thord Daniel Hedengren
    Member
    Posted 7 years ago #

    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 http://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!

  2. David Yeiser
    Member
    Posted 7 years ago #

    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:
    http://codex.wordpress.org/Template_Tags/get_the_category

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

  3. David Yeiser
    Member
    Posted 7 years ago #

    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 . ' ';

  4. Thord Daniel Hedengren
    Member
    Posted 7 years ago #

    It worked like a charm, thanks! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags