簡體   English   中英

如何顯示所有基於類別的帖子?

[英]How do I display all posts based on category?

例如,我將有4個類別。 對於每個類別,我都會顯示5個最新帖子。 我將擁有早餐,甜點,午餐和咸味食品等類別。 每個類別都會有一個“查看全部”鏈接,因此用戶可以查看所有早餐類別的帖子。 在主頁上,我將列出5條最近的帖子,當用戶單擊“查看全部”鏈接時,它將把他們鏈接到整個早餐類別。 我想要該類別中的所有早餐,如果他們單擊“查看全部”。 其他類別也一樣。 目前,我的代碼看起來像這樣,但是我被“查看全部”鏈接所困擾。 我不知道如何將其鏈接到主要類別。

 <?php get_header(); ?> <!-- recipe --> <section class="recipe-wrap"> <?php /* * Loop through Categories and Display Posts within */ $post_type = 'recipe'; $category_link = get_category_link($cat->cat_ID); // Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( 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 ) : ?> <div class="recipe-category owl-carousel-slide"> <div class="row"> <h2><?php echo $term->name; ?><a href="#">see all</a></h2> <div class="recipe-category-carousel owl-carousel owl-theme"> <?php $args = array( 'post_type' => $post_type, 'posts_per_page' => 10, //show all posts 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $term->slug, ) ) ); $posts = new WP_Query($args); if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?> <div class="item recipe-box"> <a href="<?php the_permalink(); ?>"> <img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>"> <p><?php the_title(); ?></p> </a> </div> <?php endwhile; endif; ?> </div> </section> <?php endforeach; endforeach; ?> </div> </div> </div> </section> <!-- /recipe --> <?php get_footer(); ?> 

試試下面的代碼

foreach ( $terms as $term ) {

   // Get term link by using get_term_link()

   $term_link = get_term_link( $term );

   echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';

}
<div id="mini_stream">
    <ul>
<? $args = array(
    'post_type' => 'post',
    'posts_per_page' => 4,
    'category_name'=>'Product',
);

$loop = new wp_Query($args);

while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'">';
    echo get_the_post_thumbnail($post->ID, 'category-thumb');
    the_title( '<h6>', '</h6>' );
    echo '</a>';
endwhile;

wp_reset_query(); ?>
    </ul>
</div>

=>使用這種方法可能會得到所有帖子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM