Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a WordPress theme using Bones. I would like to be able to show some text just above the title of each post if the post is either featured, password protected or private.

So far I came up with the following - this is directly inserted into index.php, single.php, archive.php and search.php (not in a function in functions.php)

<?php if(is_sticky()): ?>
  <h6 class="post-format"><?php _e('Featured', 'bonestheme'); ?></h6>
<?php endif; ?>
<?php if($post->post_password): ?>
  <h6 class="post-format"><i class="icon-lock"></i> <?php _e('Protected', 'bonestheme'); ?></h6>
<?php endif; ?>
<?php if($post->post_status == 'private'): ?>
  <h6 class="post-format"><i class="icon-ban-circle"></i> <?php _e('Private', 'bonestheme'); ?></h6>
<?php endif; ?>

The code above works fine, however, I would like to improve it and put it a function in functions.php. So far I have come up wit the following.

function print_post_format(){

if(is_sticky()):
  printf( __( '<h6 class="post-format">%s</h6>', 'bonestheme' ), 'Featured' );

else($post->post_password):
  printf( __( '<h6 class="post-format"><i class="icon-lock"></i>%s</h6>', 'bonestheme' ), 'Protected' );

else($post->post_status == 'private'):
  printf( __( '<h6 class="post-format"><i class="icon-ban-circle"></i>%s</h6>', 'bonestheme' ), 'Private' );

endif;
}

When calling this function, 'Featured' shows up, however, 'Protected' and 'Private' do not. Any help would be greatly appreciated.

Thank you, Ian

share|improve this question
    
Does it work if you add global $post; above your first if-statement? Just below your function print_post_format(){. –  hampusn Sep 20 '12 at 10:51
    
Thanks for your reply. Yes, that solves most of the problem - However, I think I have an issue in my 'if'/'else' statement - If I let the whole chunk of code run, I get the following error 'Parse error: syntax error, unexpected '(', expecting ':' in C:\...\functions.php on line 340' - line 340 being the first 'else' statement. –  Ian Muscat Sep 20 '12 at 11:01
    
Ah yes, you should use elseif() instead of else since you are checking conditions. –  hampusn Sep 20 '12 at 11:04
    
Yeah, I figured it out - I was just about to post my findings - many thanks! –  Ian Muscat Sep 20 '12 at 11:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.