Wordpress 嵌套循环打印错误的 de html 代码

Wordpress nested loop prints wrong de html code

我在 wordpress 中的嵌套循环有问题,我的代码正在打印 html 个元素,这是我的代码:

<div id="content">
    <div class="page-content">
        <ul class="menu-children">
        <?php
        $children_args = array(
            'post_type'      => 'page',
            'posts_per_page' => -1,
            'post_parent'       => $actual_page_ID,                               
            'order'             => 'ASC',
            'orderby'           => 'date',
        );

        $children = new WP_Query($children_args);
        //var_dump($children);

        if ( $children->have_posts() ) : ?>

            <?php while ( $children->have_posts() ) : $children->the_post(); ?>


            <a class="menu-child" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <li class="menu-child-li">
                    <?php the_title(); ?>
                    <?php
                    //I get the title of the child page because is the name of the category i want to retrieve
                    $the_post_title=get_the_title();
                    $products_args = array(
                        'post_type' => 'alquiladora_product',
                        'category_name' => $the_post_title
                    );

                    $products = new WP_Query($products_args);

                    if ( $products->have_posts() ) : ?>
                    <ul class="sub-menu-child">

                        <?php while ( $products->have_posts() ) : $products->the_post(); ?>
                        <a class="menu-child sub-menu-child-a" href="<?php the_permalink(); ?>">
                            <li class="menu-child-li sub-menu-child-li">
                                <?php the_title(); ?>
                            </li>
                        </a>
                        <?php endwhile; ?>
                    <?php endif; wp_reset_query(); ?>
                    </ul>
                </li>
            </a>

            <?php endwhile; ?>

        <?php endif; wp_reset_query(); ?>
        </ul>
        <script>
        $(document).ready(function(){
            /* navigation sub-menu display */
            $('ul.sub-menu-child').parent().hover(function(){
                $(this).children('.sub-menu-child').slideToggle(300);
            });
        /* end navigation menu */
        });
        </script>
    </div>
</div>

如果类别名称与页面 child 标题匹配,则应该在 ul 中打印 ul,我有 2 个示例: 在此页面中它工作正常: http://mginteractive.com.mx/alquiladora/productos-para-eventos/

但在这一个中没有: http://mginteractive.com.mx/alquiladora/productos-de-construccion/

检查第二个示例中的元素后,显示代码仅打印 -ul- 内的第一个 -li- 元素和 -ul- 容器外的其他 -li- 元素。我在循环中做错了什么?或者我该如何解决?

                if ( $products->have_posts() ) : ?>
                <ul class="sub-menu-child">

                <?php endif; wp_reset_query(); ?>
                </ul>

开始 <ul class="sub-menu-child"> 是有条件的,结束 </ul> 不是,这导致输出像

<ul class="menu-children">



            <a class="menu-child" href="http://mginteractive.com.mx/alquiladora/productos-de-construccion/modulos/" title="Módulos">
                <li class="menu-child-li">
                    Módulos                                     </ul>
                </li>
            </a>

看到模数之后的 </ul> 了吗? Módulos 没有帖子,因此 "sub-ul" 尚未打开,但已关闭...外部 <ul>.