-3

Is it possible to get the title of $currentProduct to display in the meta title by doing something like:

<?php
    $root = $_SERVER["DOCUMENT_ROOT"];
    $pageTitle = "Company Name | Our Products - <?php echo  $currentProduct->title ?>";
?>   

$productTitle = $currentProduct->title; 
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
031725
  • 1
  • 1

1 Answers1

0

Not sure about the order of code and the tags are correct but:

Assuming you converted your JSON string to a JSON object, yes you can. In PHP you have a couple of choices: Either you continue with your $currentProject as a STDClass Object or you convert it into an array:

// STDClass Object (Access with 'STDObject->key' returns value)
$currentProduct = json_decode($json_string); // Converts to STDClass Object

// Array Object (Access with Array[key] returns value)
$currentProduct = json_decode($json_string, true); // Converts to Array Object

With and STDClass Object:

1:

// Use '{' and '}' to specify the interpreter you're dealing with an object.
$pageTitle = "Company Name | Our Products - {$currentProduct->title}"; // assuming $currentProduct is a String at least

2:

// Using ' single quotes with this case is better
// but you can't put the variable inside it. (Not that you need it in this case)
$pageTitle = 'Company Name | Our Products - '.$currentProduct->title; // Concatenate the string

3:

String Operators documentation

$pageTitle .= $currentProduct->title;

And now the same but with an Array Object:

1:

// Use '{' and '}' to specify the interpreter you're dealing with an object.
$pageTitle = "Company Name | Our Products - {$currentProduct['title']}"; // assuming $currentProduct is a String at least

2:

// Using ' single quotes with this case is better
// but you can't put the variable inside it. (Not that you need it in this case)
$pageTitle = 'Company Name | Our Products - '.$currentProduct['title']; // Concatenate the string

3:

String Operators documentation

 $pageTitle .= $currentProduct['title'];

Note:

For the PHP Interpreter to —take in consideration— variables inside a string you must use " double quotes. And as for the curly braces:

«Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$.»

Check out this answer.

Community
  • 1
  • 1
James
  • 679
  • 1
  • 8
  • 22