Title: Adding post class to div with printf
Last modified: August 20, 2016

---

# Adding post class to div with printf

 *  [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * (@lostbythelake)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/)
 * I’m trying to add a post class to an opening div tag using printf, only it keeps
   on throwing an error. My code is as follows:
 * > if( ! empty( $img ) ) {
   >  printf( ‘<div’ <?php post_class(‘alignleft’); ?> ‘
   > >’); printf( ‘[%s](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/%s?output_format=md)‘,
   > get_permalink(), the_title_attribute( ‘echo=0’ ), $img ); echo ‘</div>’;
 * I’ve been racking my brain on this and can’t figure out what I’m doing wrong?
   Maybe I just can’t insert a post class into a div this way?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478575)
 * a: you can’t use php tags within `printf(); you would need to use string concatenation;
   
   b: you can’t use `post_class()` within string concatenation; you will need to
   construct something using `get_post_class()`:
 * [http://codex.wordpress.org/Function_Reference/get_post_class](http://codex.wordpress.org/Function_Reference/get_post_class)
 * why is this complicated code needed?
 *  [Chris Olbekson](https://wordpress.org/support/users/c3mdigital/)
 * (@c3mdigital)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478578)
 * Why are you using printf? Read the php [printf manual](http://php.net/manual/en/function.printf.php).
   Also php tags inside a function call parenthesis will not be parsed.
 *     ```
       if ( ! empty( $img ) {
           printf( '<div %s >', get_post_class( 'alignleft' ) );
           printf( <a href="%s" title="%s">%s</div>', get_permalink(), the_title_attribute( 'echo=0' ), esc_url( $img ) );
        }
       ```
   
 *  Thread Starter [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * (@lostbythelake)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478604)
 * Sorry, I’m quite new to php. All I am trying to do is wrap an image in a div 
   that contains the post class info so that I can style it accordingly using css.
   The original function is as follows:
 *     ```
       if( ! empty( $img ) )
       	printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
       ```
   
 * This on it’s own works just fine, however the image is output on it’s own without
   a surrounding div. My hope was to wrap that image in a div with a post class 
   so that I may style the image and the div wrapped around it based on its category.
 * Alchymyth, I imagine there is a more simple way of doing this, but I’m a little
   stumped. Any help would be greatly appreciated.
 * Chris, thanks so much for the code. However, the printf for the image is now 
   throwing an error instead. I believe there should have been a leading quote before
   the anchor tag, but that doesn’t seem to completely fix it either.
 * Thank you in advance for your time and consideration on this. It really means
   a lot.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478618)
 * try:
 *     ```
       if( ! empty( $img ) ) {
       	echo '<div '; post_class(); echo '>';
       	printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
       	echo '</div>';
       }
       ```
   
 *  Thread Starter [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * (@lostbythelake)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478625)
 * Alchymyth, thank you so much for the help! Although it works, I’ve noticed that
   some of the information from post_class is impacting some of my previously styled
   items in my css. Given that I only need the category to style the div, I was 
   wondering if I should probably be using get_the_category instead of post_class
   to avoid the extraneous information. Is there any way to use get_the_category
   in the same manner you have here instead of post class?
 * Furthermore, I need the class alignleft to be included as a class in all instances.
   If you can help me figure that out, I’m home free!
 * In effect, I need the output to be <div class”alignleft category”><img /></div
   >
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478628)
 *     ```
       if( ! empty( $img ) ) {
       	echo '<div class="alignleft'; foreach(get_the_category() as $cat) { echo ' category-'.$cat->slug; }; echo '">';
       	printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
       	echo '</div>';
       }
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_the_category](http://codex.wordpress.org/Function_Reference/get_the_category)
 *  Thread Starter [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * (@lostbythelake)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478630)
 * Ok, this one last thing. What you’ve given me totally worked! Gives me exactly
   what I asked for. Thank you for being so patient with me. However, I just noticed
   that in order for my css styling to work as expected, I also need to add the 
   category to the image class. Here’s my full function:
 *     ```
       function apparition_do_post_image() {
   
       	if ( ! is_singular() && genesis_get_option( 'content_archive_thumbnail' ) ) {
       		$img = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'alignleft post-image' ) ) );
   
       	  if( ! empty( $img ) ) {
       		  echo '<div class="alignleft'; foreach(get_the_category() as $cat) { echo ' category-'.$cat->slug; }; echo '">';
       		  printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
       		  echo '</div>';
       	  }
       	}
   
       }
       ```
   
 * You’ll notice that in the first if statement, the image is declared and the class
   is defined as ‘alignleft post-image’. I just need to add the category to that
   class and then I’m finally there.
 * I must thank you for all the help so far. I can’t thank you enough for this.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478658)
 *     ```
       function apparition_do_post_image() {
   
       	if ( ! is_singular() && genesis_get_option( 'content_archive_thumbnail' ) ) {
       		$cat_class = ''; foreach(get_the_category() as $cat) { $cat_class .= ' category-'.$cat->slug; };
       		$img = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'alignleft post-image' . $cat_class ) ) );
   
       	  if( ! empty( $img ) ) {
       		  echo '<div class="alignleft' . $cat_class . '">';
       		  printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
       		  echo '</div>';
       	  }
       	}
   
       }
       ```
   
 *  Thread Starter [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * (@lostbythelake)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478675)
 * Alchymyth,
 * Thank you, thank you, thank you!!! And just for good measure, another thank you.
 * This worked so well I figured out that I only needed the category tied to the
   image class to style my elements appropriately! Makes my css much cleaner and
   my pages less cluttered.
 * Not sure what part of the world you are in, but if I’m there, I owe you a beer
   🙂
 * Cheers,
 * Karl

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Adding post class to div with printf’ is closed to new replies.

## Tags

 * [div-class](https://wordpress.org/support/topic-tag/div-class/)
 * [printf](https://wordpress.org/support/topic-tag/printf/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [lostbythelake](https://wordpress.org/support/users/lostbythelake/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/adding-post-class-to-div-with-printf/#post-3478675)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
