循环访问自定义分类并显示帖子

时间:2010-08-14 作者:curtismchale

虽然我能够让它在普通WP类别中工作,但我无法让它在自定义分类法中工作。

我想循环浏览每个自定义分类法(在我的例子中是类别),并为每个分类法生成许多帖子。

输出示例如下:

Category 1

post from category one
post from category one

read more category one


Category 2

post from category two
post from category two

read more category two
当然,它会在自定义帖子类型的任何可用分类法中重复。

4 个回复
最合适的回答,由SO网友:Joe Hoyle 整理而成

我想我会提供另一个答案,因为上面的答案有点粗糙,而且我还添加了另一个层,它可以获取post\\u类型的所有分类法。

$post_type = \'post\';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy );

    foreach( $terms as $term ) : 

        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );

        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
            //Do you general query loop here  
        endwhile; endif;

    endforeach;

endforeach;
建议将找到的每个帖子添加到$post__not_in 数组,这样你就可以把它传递给WP_Query 防止重复的帖子通过。

SO网友:Maidul

你在找这个吗?

<?php query_posts(array(\'post_type\' => \'post type name\', \'Taxonomy slug\' => $term->slug ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php endif; ?>

How to create Custom Taxonomy loop

希望能有所帮助

SO网友:drjorgepolanco

在函数中复制并粘贴此函数。php

if ( ! function_exists( \'display_all_products_from_all_categories\' ) ) {

    function display_all_products_from_all_categories() {

        // Get all the categories for Custom Post Type Product
        $args = array( 
            \'post_type\' => \'product\', 
            \'orderby\' => \'id\', 
            \'order\' => \'ASC\' 
        );

        $categories = get_categories( $args );

        foreach ($categories as $category) {
            ?>
            <div class="<?php echo $category->slug; ?>">
                <!-- Get the category title -->
                <h3 class="title"><?php echo $category->name; ?></h3>

                <!-- Get the category description -->
                <div class="description">
                    <p><?php echo category_description( get_category_by_slug($category->slug)->term_id ); ?></p>
                </div>

                <ul class="mhc-product-grid">

                    <?php
                        // Get all the products of each specific category
                        $product_args = array(
                            \'post_type\'     => \'product\',
                            \'orderby\'      => \'id\',
                            \'order\'         => \'ASC\',
                            \'post_status\'   => \'publish\',
                            \'category_name\' => $category->slug //passing the slug of the current category
                        );

                        $product_list = new WP_Query ( $product_args );

                    ?>

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

                        <li class="product <?php the_field( \'product_flavor\' ); ?>">
                            <a href="<?php the_permalink(); ?>" class="product-link">

                                <!-- if the post has an image, show it -->
                                <?php if( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail( \'full\', array( \'class\' => \'img\', \'alt\' => get_the_title() ) ); ?>
                                <?php endif; ?>

                                <!-- custom fields: product_flavor, product_description ... -->
                                <h3 class="title <?php the_field( \'product_flavor\' ); ?>"><?php the_title(); ?></h3>
                                <p class="description"><?php the_field( \'product_description\' ); ?></p>
                            </a>
                        </li>

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

            </div>
            <?php
        }
    }
}
然后从模板中的任意位置调用它,方法是:

display_all_products_from_all_categories();

SO网友:bueltge

请检查此示例;为你的分类法创建一个自己的循环。您还可以在foreach循环中使用它来使用所有类别。或者您必须创建自己的sql查询。

<?php
$taxonomies = get_the_term_list($post->ID, \'YOUR_TAXONOMIE\', \'\', \'\', \'\');
$taxonomies = explode(\'>\', $taxonomies);
$taxonomies = $taxonomies[1];
$myq = new WP_Query(\'your_taxonomie = \'.$taxonomies); 
if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); // the loop ?>

            <?php the_title();?>
            <?php the_content();?>

<?php endwhile; else:?>

<?php endif;?>

结束

相关推荐