2

I have the following problem. I am using Advanced Custom Fields for Wordpress to create a subtitle field for a post. I like to give this subtitle some styling but my HTML code within the IF-statement doesn't show on the page. The $subtitle does show.

<?php $subtitle = the_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

I spent hours searching for similar problems but couldn't find any solutions. So this must probably be a rookie mistake on my part.

Solution

<?php $subtitle = get_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

Changed the_field() to get_field(). Kuddo's to Aditya Vikas!

3 Answers 3

3

You should echo/print it (The subtitle variable).

<div class="post-sub-title"><?php echo $subtitle; ?></div>
Sign up to request clarification or add additional context in comments.

2 Comments

Ok thank you for your response but this doesn't fix the problem for the HTML not being shown.
Then I guess the length of the data in subtitle is not greater than zero after trim function is called. Or you must have a bug somewhere else in your code, not the part you provided. Edit: Try print it without using the if statement and see if it can print.
2

try using this piece of code :

<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?=$subtitle?></div>
<?php endif; ?>

instead of this :

<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

and also one more thing !

the_field() is not a default WordPress function

the 'whatever' plugin you are using might have a corresponding function:

get_field()

2 Comments

I tried what you suggested. But it didn't work. The <div> still doesn't show. Only the variable $subtitle (wich contains some plain text) does.
Changing the the_field() to get_field() fixed to problem! Thank you very much!
0

try this one:

<?php $subtitle = get_field('subtitle'); ?>
<?php if(!empty(trim($subtitle))): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.