1

I use the underscores.me framework and in my content.php (the template displaying the loop of posts) I have the following code:

<div class="article-text dot-ellipsis">
    <div class="article-title clickablediv">
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><br>
    </div>

    <div class="excerpt-index clickablediv">
        <a href="<?php echo get_permalink(); ?>"></a>

        <?php the_date(); echo " - ";
        $my_excerpt = get_the_excerpt();
        if ( '' != $my_excerpt ) {
        }
        echo $my_excerpt; 
        ?>
    </div>
</div>  

This enables me to style every post. However I want to be able to style each post format differently so that e.g "status" posts and regular posts get different markup. I want to do this without CSS!

I am not sure if this makes any sense but, in other words is there a way to do:

If post format is "status" then apply the following markup -->> Something

If post format is "quote" then apply the following markup --->> Something

and so on... for all the formats available in Wordpress by default.

  • please please please don't shout. we hear you (aka not blind and being able to read) even when you don't speak in bold – Mark Kaplun Jun 10 '16 at 16:42
  • Sorry about that. I just find it too often that people do not read the whole question and reply with "why don't you just do this and that" when I have already specified this. – Arete Jun 10 '16 at 16:45
  • I think the title of the question was explicit enough and didn't need the additional emphasize ;) – Mark Kaplun Jun 10 '16 at 16:47
3

Since you need a specific HTML structure let's keep things clean and do what The Codex suggests - break it into template parts based on the post format. Let's create a subfolder in your theme to store these post formats, maybe POST_TYPE_SLUG-formats so it's clear what is in this folder ( Replace the uppercase with your actual post type slug ). Then we can store file in this folder like format-aside.php with all the markup you need.

Finally, our loop would look like this:

<?php
    if( have_posts() ) {
        while( have_posts() ) {
            the_post();
            $format = get_post_format();
            $format = ( FALSE !== $format ) ? $format : 'default';

            get_template_part( 'POST_TYPE_SLUG-formats/format', $format );
        }
    }
?>

IF our post type was just the built-in post we would have a subfolder in our theme called post-formats and inside a few format files like: post-formats/format-aside.php and post-formats/format-chat.php. In the case that, for whatever reason get_post_format() fails and returns false it's probably a good idea to add post-formats/format-default.php as a fallback.

In your format file you have full access to the loop functions like the_content() and the_title().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.