user3615681
user3615681

Reputation: 271

PHP code within IF statement

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

Upvotes: 0

Views: 70

Answers (3)

user3615681
user3615681

Reputation: 271

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>";
}
?>

Upvotes: 0

maxhb
maxhb

Reputation: 8865

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";
}
?>

Upvotes: 1

AnkiiG
AnkiiG

Reputation: 3488

You have error in else part. Replace :

"Testimonial On"

to

echo "Testimonial On";

Upvotes: 0

Related Questions