Alex Quinn
Alex Quinn

Reputation: 29

Create link from php variable

I'm using WooCommerce and I'm trying to create a simple button that links to a supplier's website. The website url is an attribute within each product but I can't figure out the painfully basic way of using a php value as a url.

At the minute I'm using the following to display the text of an attribute:

<?php
$suppliervalues = get_the_terms( $product->id, 'pa_supplier');

  foreach ( $suppliervalues as $suppliervalue ) {
  echo $suppliervalue->name;
    }
?>

All I think I need to do is convert this so that it says:

<?php
$suppliervalues = get_the_terms( $product->id, 'pa_supplier');

  foreach ( $suppliervalues as $suppliervalue ) {
  echo <a href="$suppliervalue->name;">Click here</a>
    }
?>

Thanks in advance!

Upvotes: 1

Views: 79

Answers (3)

meda
meda

Reputation: 45490

You can create links the following ways:

echo '<a href="'.$suppliervalue->name.'">Click here</a>';

or

echo '<a href="{$suppliervalue->name}">Click here</a>';

or

$link = '<a href="';
$link.= $suppliervalue->name;
$link.= '">Click here</a>';
echo $link;

or

$link= sprintf("<a href='%s'>Click here</a>",$suppliervalue->name);
echo $link;

Upvotes: 0

TMH
TMH

Reputation: 6246

You've not quite got the echo syntax correct.

<?php
$suppliervalues = get_the_terms( $product->id, 'pa_supplier');

foreach ( $suppliervalues as $suppliervalue ) {
    echo '<a href="' . $suppliervalue->name . '">Click here</a>';
}
?>

Give that a go.

Upvotes: 1

Dan Smith
Dan Smith

Reputation: 5685

This is the correct syntax

<?php
$suppliervalues = get_the_terms( $product->id, 'pa_supplier');

foreach ( $suppliervalues as $suppliervalue )
{
    echo '<a href="'.$suppliervalue->name.'">Click here</a>';
}
?>

Upvotes: 1

Related Questions