[英]WordPress/PHP if statement not executing
所以我试图在PHP if语句中返回一行HTML,但是由于某种原因它没有执行。
<?php
$args_commercial = array(
'post_type' => 'sidebar_post'
);
$query_commercial = new WP_Query( $args_commercial );
?>
<ul>
<?php if (have_posts() ) : while ($query_commercial->have_posts() ) : $query_commercial->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
?>
<li data-id="0">
<?php if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<?php
$page_field = strtolower(get_field( 'page' ));
$page_title = strtolower(get_the_title());
if ( $page_field == $page_title ): ?>
<a href="<?php echo get_field( 'link' ); ?>" style="background-image: url(<?php echo $thumb_url; ?>)"><span><?php the_title(); ?></span></a>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
<?php endwhile; endif; ?>
</ul>
因此,为了解释一下,我在自定义帖子类型(带有帖子的边栏)中循环,并且在该自定义帖子类型中可以选择要显示自定义帖子类型的页面。
这就是$ page_field变量代表的内容。 $ page_title只是页面的标题。 因此,从理论上讲,如果用户选择的页面字段和页面的实际标题相同,则将打印HTML,然后显示侧栏。
现在由于某种原因,即使$ page_title和$ page_field是完全相同的值,if块中的HTML也不执行(我通过回显它们进行了测试)。
我真的不确定如何进一步调试并确保HTML连续显示。 任何帮助将不胜感激
谢谢。
看起来循环让我很烦。 我首先循环浏览页面本身,获得页面标题,然后再循环浏览自定义帖子类型。 这让if块执行。 这是代码:
<?php
$args_commercial = array(
'post_type' => 'sidebar_post'
);
$query_commercial = new WP_Query( $args_commercial );
?>
<ul>
<?php if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<?php
$page_title = strtolower(get_the_title());
?>
<?php endwhile; endif; ?>
<?php if (have_posts() ) : while ($query_commercial->have_posts() ) : $query_commercial->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
?>
<li data-id="0">
<?php
$page_field = strtolower(get_field( 'page' ));
if ( trim($page_field) == trim($page_title) ): ?>
<a href="<?php echo get_field( 'link' ); ?>" style="background-image: url(<?php echo $thumb_url; ?>)"><span><?php the_title(); ?></span></a>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.