質問

私はこれを通常の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

もちろん、カスタムポストタイプの利用可能な分類法を繰り返します。

役に立ちましたか?

解決

上記の答えは少しハッキーであるため、別の答えを提供すると思いました。また、Post_Typeのすべての分類法を取得する別のレイヤーを追加しました。

$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 重複した投稿が通過するのを防ぐため。

他のヒント

これをお探しですか?

<?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; ?>

カスタム分類ループを作成する方法

それが役立つことを願っています

この関数をfunctions.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();

この例を確認してください。分類局のための独自のループを作成します。これをforeachループで使用して、すべてのカテゴリを使用することもできます。または、独自のSQL-Queryを作成する必要があります。

<?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;?>
ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top