0

I have loop and template for post

if ( have_posts() ) : ?>
    <div class="container">
        <div class="row">
            <?php while ( have_posts() ) : the_post();?>
                <?php get_template_part( 'template-parts/post', get_post_format() );?>
            <?php endwhile; ?>
        </div>
    </div>
<?php else :?>
    <?php get_template_part( 'template-parts/content', 'none' );?>
<?php endif; ?>

and I want to use post format and add special html code for eash post format, so i add to my post template (post.php in template-parts):

<?php if ( get_post_format() == 'link' ) : ?>
   //code here
<?php endif;?>

but it doesnt work.

So is it possible to use get_post_format() function in post template file? Or how to check current post format inside post template file?

1
  • The shared code is working fine for me in both template files single.php and template parts like content.php so you must be using the code wrong. If you share some more information about how you are using it then i can help you to make it work. Commented Apr 27, 2017 at 12:45

1 Answer 1

1

If you're outside the loop, pass a post ID.

$format = get_post_format( $post_id );

To guard against missing formats, add a default to your template:

$format = get_post_format() ? : 'standard';

Then you can use your same IF statement:

if ( $format == 'link' ) :

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.