IF语句中的PHP代码

时间:2016-02-10 11:05:25

标签: php wordpress

我正在寻求一些帮助,因为我在尝试将PHP代码放入IF语句时遇到了一些困难:

我有以下代码:

<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>

我正在尝试将上述代码放在下面的代码中,其中显示“Testimonial off”,但不是“Testimonial On”部分。

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

每次尝试我都会遇到PHP错误,有人可以帮我解决吗?

我试图将两个代码合并在一起,但我现在可以在else语句中显示侧边栏:

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

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

你必须知道正确打开和关闭php标签:

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

答案 1 :(得分:0)

你在其他方面有错误。替换:

"Testimonial On"

echo "Testimonial On";

答案 2 :(得分:0)

我设法通过以下方式让它发挥作用:

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