0

I use a <span> inside the loop and I would like to get each post format the post is containing as a class in separate spans.

Example:

If the post format is video and private: I would like to render:

<span class="format-video"></span>
<span class="status-private"></span>

So far I have tried <span <?php get_post_format_string($class); ?>></span> which did not work for me because this will render all the post formats and status in one span, not in different spans...

1

2 Answers 2

1

Every post has a maximum of one format and one status. To echo the format you need:

if (get_post_format($post->ID))
    echo '<span class="format-' . get_post_format($post->ID); . '"></span>';

To echo the status you need this:

if (get_post_status($post->ID))
    echo '<span class="status-' . get_post_status($post->ID); . '"></span>';

Hopefully this is what you need.

2
  • Where should I place this code? I need to add the code outside <?php the_content
    – Arete
    May 20, 2016 at 20:34
  • In the same place where your non working code is...
    – cjbj
    May 20, 2016 at 21:26
1

You can also try <span class="<?php echo get_post_format($post->ID); ?>"></span> Additional details available here

Edited from here:

Well according to the link I provided, get_post_format() give you the post format post the post_type post.

So if you want to use it in loop, you could try

while ( have_posts() ) : the_post();
   echo '<span class="'.get_post_format().'">Your content</span>';   
endwhile;

The above code will output a class in you're span based on the post format. For instance, if you're post is an image format the output will be like

<span class="image">You're content</span>

I hope this make sense

1
  • Please elaborate...
    – Arete
    May 20, 2016 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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