Using Printf Command - Width and Height problem

by 7 replies
9
Hey Warriors,

I am having a problem specifying the width and height using the printf command.

The code is as below:

Code:
printf('
						<a href="'.get_term_link( $term ).'" style="color:%s;" data-javo-category="%s">
							<img src="%s" style="width:300px; height:200px;">
							<span style="font-size:%s'.'px;">%s</span>
							<div class="categories-wrap-shadow"></div>
							<div class="inner-meta %s">
<!--								<div>%s</div> -->
<!--								<small>%s</small> -->
								<div class="cat-title">'.$term->name.'</div>
								<div class="cat-des">'.$term->description.'</div>

				<?php endif; ?>
								</div>
						</a>'
						, $title_text_color
						, $term->term_id
						, $featured
						, $category_size
						, $term->name
						, ( $display_item_count == 'hide' ? 'hidden' : '' )
						, $term->name
						, javo_get_count_in_taxonomy( $term->term_id )
					);

When I perform speed analysis using GTMetrix, it says that "image(s) are missing width and/or height attributes." If I put it as follows:

Code:
width="300" and height="200"
then I get printf function error saying too few arguments in printf function.

If I delete the width and height specifications from here and put it in the stylesheet, still it does have the same warning in GTMetrix

Please help me to correctly specify the size in this command so that it doesn't give warnings in GTMetrix
#programming #command #height #printf #problem #width
  • I assume you're doing this in PHP.

    You are trying to embed some special characters (double quotes) in a string. C languages start and end strings with quotes. If you put a quote in the middle, the language sees that as the end of the string. You need to escape the special character so that the compiler/interpreter sees it as a literal rather than the end of the string.

    Try escaping the special characters in the string using the \ character. So to escape a double quote it would look like: "width=\"300\""

    See here for more info on printf: PHP: printf - Manual
    • [1] reply
    • Thank you for your suggestion but it doesn't work.

      I put the code as below:
      <img src="%s" "width=\"300\"" "height=\"200\"">
      • [2] replies
  • Nope, it didn't work either.

    When I put that code, it doesn't recognize the command and it goes to the default image size.
    • [1] reply
    • Try to post all code you have in that file so we can have a better look, the problem could be somewhere else, not right there. I know that the code I gave to you must work, because I have tested that before posted reply.
      • [1] reply

Next Topics on Trending Feed