WordPress/PHP if语句未执行

WordPress/PHP if语句未执行,php,html,wordpress,Php,Html,Wordpress,因此,我试图在PHP if语句中返回一行HTML,但由于某些原因,它没有执行 <?php $args_commercial = array( 'post_type' => 'sidebar_post' ); $query_commercial = new WP_Query( $args_commercial

因此,我试图在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_字段变量所表示的内容。$page\u标题只是页面的标题。因此,理论上,如果用户选择的页面字段和页面的实际标题相同,则会打印HTML,然后显示侧边栏

现在由于某种原因,if块中的HTML没有执行,即使$page_title和$page_字段的值完全相同(我通过回显来测试它们)

我真的不知道如何进一步调试并确保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>


var\u dump($page\u字段);变量转储($page\u title);确保这些值与您期望的值相同。@Kaylined I var_转储了它们,它们都打印
string(0)”“string(22)”commercial air charter“
,因此您的第一个var为空。if(“==”商业航空包机“)无效。@Kaylined,但它们都是相同的,即它们都是var dump
string(0)”“string(22)”商业航空包机“
,所以不应该执行if块吗?白色字符呢?尝试更改为if(trim($page\u field)=trim($page\u title)):