如何在wordpress中循环ul内部的ul


How to loop the ul inside ul in wordpress?

在父ul和子ul中实现wordpress循环是否正确?我遇到了一些问题,因为父项输出正确,但子项输出不正确。子级的帖子标题仅在一个父级ul中输出。

<ul class="multi-category">
<?php query_posts(array('post_type' => array('post'),'posts_per_page' => 3)); ?>
                    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>   
    <li>
                    <h3>
                        <a href="<?php the_permalink(); ?>">Food</a></h3>
        <div class="multi-category-image">
                            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Fun creations with potatoes and rice">
                                <?php if ( has_post_thumbnail() ) {
                            the_post_thumbnail(420,470);
                            } else { ?>
                            <img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
                            <?php } ?></a>
                            <div class="multi-category-text">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </div><!--multi-category-text-->
        </div><!--multi-category-image-->
        <?php endwhile; // end of the loop. ?>
                    <?php endif; ?> 

                    <div class="multi-category-headlines">
            <ul class="multi-category-headlines">
                    <?php query_posts(array('post_type' => array('post'),'posts_per_page' => 4)); ?>
                    <?php if(have_posts()) : while(have_posts()) : the_post(); ?> 
                                    <li>
                                        <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                    </li>
                                <?php endwhile; // end of the loop. ?>
                    <?php endif; ?> 
                                </ul>
        </div><!--multi-category-headlines-->
    </li>
</ul>

我认为您不应该使用query_posts。它扰乱了wordpress循环。

改为使用Wp_query:

( ..parent loop .. ..the_title()..etc )
$args = array(
    'numberposts'     => 10,
    'post_type'       => 'my-post-type',
    'post_status'     => 'publish' );
$myloop = new WP_Query($args);  //<-- do the query for my subloop
//  Now use the $myloop to form the subloop
//  In loop you can forget $myloop and juse just the wordpress template tags like in 
//  the main loop.
if ( $myloop->have_posts() ): while ( $myloop->have_posts() ) : $myloop->the_post();
     the_title();    // <-- wordpress now knows it's for the $myloop loop  
endwhile;
endif;
wp_reset_postdata();  //<<-- return to the main loop. don't forget it

( ..parent loop .. )

希望这能有所帮助。请参阅编码。这真的很简单。