WordPress.org

Ready to get started?Download WordPress

Forums

Adding post class to div with printf (10 posts)

  1. lostbythelake
    Member
    Posted 1 year ago #

    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', 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?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

    why is this complicated code needed?

  3. Why are you using printf? Read the php printf manual. 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 ) );
     }
  4. lostbythelake
    Member
    Posted 1 year ago #

    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.

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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>';
    }
  6. lostbythelake
    Member
    Posted 1 year ago #

    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>

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

  8. lostbythelake
    Member
    Posted 1 year ago #

    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.

  9. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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>';
    	  }
    	}
    
    }
  10. lostbythelake
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic