【问题标题】:How to loop the ul inside ul in wordpress?如何在wordpress中循环ul里面的ul?
【发布时间】:2014-10-13 11:06:08
【问题描述】:

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

【问题讨论】:

  • 你的问题有点不清楚。你到底想做什么,解析这个还是生成这个?
  • 您遇到了什么问题?
  • 我很困惑如何在我的父母
      和我的孩子
        在 wordpress 帖子中插入循环

标签: php html wordpress


【解决方案1】:

我认为你不应该使用query_postsIt messes up the wordpress loop.

改用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 .. )

希望这会有所帮助。见法典。真的很简单。

【讨论】:

  • 子循环帖子怎么样?
    • 我不明白你的意思。要获取某些帖子的孩子,您必须将父母的 id 传递给查询。但我认为帖子在 wordpress 中不是分层的。页是。这些是页面吗?
    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多