简体   繁体   English

如何显示所有基于类别的帖子?

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

For example i will have 4 categories. 例如,我将有4个类别。 For each category i will show 5 recent post. 对于每个类别,我都会显示5个最新帖子。 I will have category such as breakfast, dessert, lunch and savory food. 我将拥有早餐,甜点,午餐和咸味食品等类别。 There will be a "see all" link for each category, so the user can see all the breakfast category post. 每个类别都会有一个“查看全部”链接,因此用户可以查看所有早餐类别的帖子。 On the main page i will list 5 recent post and when the users click the "see all" link it will link them to the whole breakfast category. 在主页上,我将列出5条最近的帖子,当用户单击“查看全部”链接时,它将把他们链接到整个早餐类别。 I want all breakfast in that category if the they click "see all". 我想要该类别中的所有早餐,如果他们单击“查看全部”。 The same for other categories. 其他类别也一样。 Currently my code looks like this, but i'm stuck with the "see all" link. 目前,我的代码看起来像这样,但是我被“查看全部”链接所困扰。 I don't know how to link it to main category. 我不知道如何将其链接到主要类别。

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

Try below code 试试下面的代码

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>

=> Using this method may you get all post =>使用这种方法可能会得到所有帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM