0

I'm after a bit of help as I'm having a bit of difficulty trying to put php code in an IF statement:

I have the following code:

<aside class="sidebar top">

<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php 
$image = get_field('quote_image');

if( !empty($image) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?> 
</div>

<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>          
<?php endif; ?>     

</aside>

I'm trying to put the above code in the below code, where it says "Testimonial off" but not in the "Testimonial On" section.

<?php 
$values = get_field( "display_testimonial" );
if ( $values ) {
echo "Testimonial Off";
} else {
echo "Testimonial On";
}
?>

Every time I try I'm getting PHP errors, can anyone help me out?

I have tried to merge the two codes together but I can get the sidebar to show in the else statement now:

<?php 
$values = get_field( "display_testimonial" );
if ( $values ) {
?>
<aside class="sidebar top">
<?php dynamic_sidebar( 'About-Sidebar' ); ?>

<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php 
$image = get_field('quote_image');

if( !empty($image) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?> 
</div>

<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>          
<?php endif; ?>     

</aside>
<?php
} else {
("<?php dynamic_sidebar( 'About-Sidebar' ); ?>");
}
?>

Thanks for your help

6
  • Sorry for asking, but does this file has .php extension? Commented Feb 10, 2016 at 11:08
  • echo the variable $values and check whether there is some value coming from get_field(); Commented Feb 10, 2016 at 11:09
  • Hi @OgnjenBabic Yes its a PHP file Commented Feb 10, 2016 at 11:09
  • 1
    "Every time I try I'm getting PHP errors, can anyone help me out?"... what are these errors? Commented Feb 10, 2016 at 11:12
  • 1
    See my answer for the correct way of merging both snippets. Commented Feb 10, 2016 at 11:18

3 Answers 3

1

You have to be aware of correct opening and closing php tags:

<?php 
$values = get_field( "display_testimonial" );
if ( $values ) {
  // ADDED CLOSING PHP TAG
  ?>
<aside class="sidebar top">

<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php 
$image = get_field('quote_image');

if( !empty($image) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?> 
</div>

<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>          
<?php endif; ?>     

</aside>
  <?php
  // ADDED OPENING PHP TAG
} else {
echo "Testimonial On";
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, how would I add php code to the else{} statement eg how do I call the <?php get_footer(); ?> within the else{}? would it be: else{<?php get_footer();?>
That would work in principle. But if you mix to manx opening and closing php tags then you'll loose orientation in your code file. Instead of else{<?php get_footer();?> you could simply use else { get_footer();}. In general it is good practice to separate the logic (controller/model) and the output (view).
0

You have error in else part. Replace :

"Testimonial On"

to

echo "Testimonial On";

Comments

0

I managed to get it to work by doing the below:

<?php 
$values = get_field( "display_testimonial" );
if ( $values ) {
 ?>
<aside class="sidebar top">
<?php dynamic_sidebar( 'About-Sidebar' ); ?>

<?php if(get_field('quote-text')): ?>
 <div id="sidebar-testimonials">
<div class="quote-image">
<?php 
 $image = get_field('quote_image');

 if( !empty($image) ): ?>

 <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?> 
</div>

<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span> 
<?php the_field('quote-name'); ?>
</span>
</div>          
<?php endif; ?>     

</aside>
<?php
} else { 
echo "<aside class='sidebar top'>";
dynamic_sidebar( 'About-Sidebar' ); 
echo "</aside>";
}
?>

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.