-1

I'm using the plugin Advanced Custom Fields. The following example code is given on the acf website for if statements.

<?php

if(get_field('field_name'))
{
    echo '<p>' . get_field('field_name') . '</p>';
}

?>

I have tried adding an else statement like so:

<?php
if(get_field('main_title'))
{
    echo '<h1>' . get_field('main_title') . '</h1>';
}
else 
{
    echo '<h1>' . the_title() . '</h1>' ;
}
?>

The snippet works perfectly if I enter data into the main_title field in WP Admin, but if it's blank, the default page title is outside the <h1> tags.

Thank you for your patience. I'm a bit of a PHP noob!

0

You are using version of template tag that echoes result immediately and so unsuitable to concatenate in string. You should be using get_the_title() for this purpose,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.