Remon
Remon

Reputation: 1

How do I use single quotes inside single quotes php?

Can anyone explain how to make this code work?

echo '<li class="list-clients__item">
      <img src="<?php get_field('brand_logo'); ?>" height="75" width="168" alt="'.$category->cat_name.'"> 
      <span class="helper"></span> 
      </li>';

Upvotes: 0

Views: 1500

Answers (4)

Mikel Wohlschlegel
Mikel Wohlschlegel

Reputation: 1476

You have nested php tags inside a php function call.

<?php get_field('brand_logo'); ?>

This is to execute php within dynamic html (e.g .phtml).

Stay with dynamic html

<li class="list-clients__item"> 
  <img src="<?php echo get_field('brand_logo'); ?>" height="75" width="168" alt="<?php echo $category->cat_name; ?>">
  <span class="helper"></span> 
</li>

or remove the nesting php tags

echo '<li class=\"list-clients__item\"> 
        <img src=\"' . get_field('brand_logo') . '\" height=\"75\" width=\"168\" alt=\"' . $category->cat_name . '\"> 
        <span class=\"helper\"></span> 
      </li>';

Upvotes: 1

Vidal
Vidal

Reputation: 2621

To make your code work, remove the php tag.

Also to excape the quotes you use the \ (escape character).

ex. echo 'tom\'s rest'

echo '<li class="list-clients__item">
      <img src="'.get_field('brand_logo') .'" height="75" width="168" alt="'.$category->cat_name.'"> 
      <span class="helper"></span> 
      </li>';

Upvotes: 0

dearsina
dearsina

Reputation: 5192

Whenever I have to embed a bunch of variables and functions into a longish string (of HTML), I find using the following format easier to read:

$brand_logo = get_field('brand_logo');
echo <<<EOF
<li class="list-clients__item">
    <img src="{$brand_logo}" height="75" width="168" alt="{$category->cat_name}">
    <span class="helper"></span>
</li>
EOF;

It's called the heredoc format.

Upvotes: 0

Ulugbek Nuriddinov
Ulugbek Nuriddinov

Reputation: 171

Try this one:

echo '<li class="list-clients__item"> <img src="'. get_field('brand_logo', 'category_' . $category->cat_ID) .'" height="75" width="168" alt="'.$category->cat_name.'"> <span class="helper"></span> </li>';

Upvotes: 0

Related Questions