【问题标题】:PHP/Wordpress: list posts per sub-categoryPHP/Wordpress:列出每个子类别的帖子
【发布时间】:2011-12-03 20:33:54
【问题描述】:

在下面的代码中有一个循环显示给定类别下的所有产品:

        <?php        

            wp_reset_query();
            query_posts($query_string . '&posts_per_page=15&paged=' . $paged);
            if (have_posts()) :
            while ( have_posts() ) : the_post();
                $price =  get_post_meta(get_the_ID(), 'inception_price', true);

        ?>

            <div class="omc-product-listing">
                <a href="<?php the_permalink();?>">
                    <?php 

                        if(has_post_thumbnail()) {
                        the_post_thumbnail('product', array('class' => 'omc-product-frame'));  
                        } else {
                    ?>

                    <img src="<?php echo get_template_directory_uri() ;?>/images/no-image.png" width="170" height="170" class="omc-product-frame" alt="no photo" />

                    <?php } ?>

                </a>

                <span class="omc-listing-header"><a href="<?php the_permalink();?>"><?php the_title();?></a></span>

                <span class="omc-listing-price"><?php echo($price);?></span>

                <span class="omc-listing-more"><a href="<?php the_permalink();?>">&raquo;</a></span>

            </div><!-- /omc-product-listing -->

        <?php endwhile;  ?>

        <br class="clear" /> 

        <div class="product-pagination">

            <?php  kriesi_pagination(); ?>    

        </div>                        

        <br class="clear" /> 

        <?php endif; wp_reset_query(); ?>

在这个循环中,我想提取给定类别的每个产品(这就是代码现在所做的),然后显示“每个子类别”,如下所示:

对于类别出版物:

书籍:

  • 第 33 册
  • 第 32 册
  • 书 1
  • ...

移动应用:

  • 应用 12
  • 应用 76
  • ...

...

我认为上面的代码需要一个foreach 循环,如下所示,但我不知道在这种情况下如何实现它。

            <?php
            // get all the categories from the database
            $cats = get_categories();

                // loop through the categories
                foreach ($cats as $cat) {
                    // setup the categories ID
                    $cat_id= $cat->term_id;
                    // Make a header for the categories
                    echo "<h2>".$cat->name."</h2>";
                    // create a custom wordpress query
                    query_posts("cat=$cat_id&post_per_page=100");
                    // start the wordpress loop!
                    if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <?php // create our link now that the post is setup ?>
                        <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                        <?php echo '<hr/>'; ?>

                    <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
                <?php } // done the foreach statement ?>

【问题讨论】:

    标签: php wordpress loops foreach categories


    【解决方案1】:

    我已将此请求发送至 wpquestions.com。 Abdessamad Idrissi 刚刚解决了这个问题。答案太长,无法在此处复制,所以我在此处发布discussion linkcode link,以防有人有同样的需求。

    【讨论】:

    • +1 供您自学并为他人提供链接。继续前进。
    • 感谢 Awais,不胜感激。最新版代码here.
    【解决方案2】:

    您可以尝试以下方法:

    $cats = get_categories();
    
    foreach ($cats as $cat) :
    
    // setup the categories ID
     $cat_id= $cat->term_id;
    // Make a header for the categories
    echo "<h2>".$cat->name."</h2>";
    
    $args = array( 'cat' => $cat_id, 'posts_per_page' => 100 );
    
    $posts = get_posts($args);
    
    if($posts) :
    
    foreach($posts as $post) : setup_postdata($post); ?>
    
    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    
    <?php endforeach; // foreach($posts)
    
    endif; // if($posts)
    
    endforeach; // foreach($cats)
    

    没有对此进行测试,但它应该能让你朝着正确的方向前进!

    【讨论】:

    • 谢谢戴夫,请看下面我的 cmets。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多