0

I want to echo this title variable inside the h1 tag, but I get an error message. Can you please tell me what I'm doing wrong?

<?php 
  $title = get_field ('page_title');
  if ($title):
  <h1> echo ($title); </h1> 
  endif;
?>
1
  • It would help us if you included the error message you're seeing. Commented Jun 14, 2021 at 1:19

3 Answers 3

2

This line is wrong:

   <h1> echo ($title); </h1> 

PHP won't understand the HTML tags so it complains about them.

There are a couple of ways you might do this, but I'd suggest replacing the entire line with

        echo "<h1>$title</h1>";

PHP will insert the current value of $title in the string and echo the result.

Reference: PHP double-quoted Strings

Sign up to request clarification or add additional context in comments.

Comments

2

This you code with correcting :

<?php 
    $title = get_field ('page_title');
    if (isset($title)){
    echo'<h1>'.$title.'</h1>'; 
    }
    end_if;
   

Comments

1

I changed the code as per the suggestion, and it is working. I hope it is correct.

<?php
$title = get_field('page_title');
if ($title) :
  echo "<h1> $title </h1>";
endif;
?>  

<?php
$input = get_field('input');
if ($input) :
  echo " <a href= 'mailto: ($input)' > $input </a> ";
endif;
?>
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.