如何创建类别页面?

How do I create the category pages?

我正在做类似的事情。我有一个食谱博客,我会有不同的类别,例如早餐、甜点、午餐等...但我不知道如何 link 将它们添加到类别页面。例如,如果用户单击早餐类别 "see all",它将显示早餐类别中的所有早餐 post。所以这意味着它将 link 到主早餐类别页面。对于每个类别,我将显示 10 个最近的 post,因此用户不会看到全部 post。因此,如果他们单击 "see all" link,他们可以选择查看所有 post。它将 link 他们与所有早餐一起进入主类别页面 post。当然,如果他们点击甜点 "see all" link,它会 link 他们到甜点类别页面,他们可以看到所有甜点 posts.

这是我当前的代码: (但是我应该为 "see all" link 添加什么 php 代码?如果你看一下我的 "see all" 标签,它现在是 #。它不是 link 到某个地方,但我如何让它动态地 link 到类别页面。例如,如果我在甜点区,如果我单击 "see all",它将 link 到甜点类别。 )

也请看看我的设计

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

?>

请看我提供的例子,这样你就知道我在说什么了。 我附上了一些图片和 link.

这是 link 的例子。

这是博客link

https://iamsteve.me/blog

Design Category link

https://iamsteve.me/blog/category/design

An example what I want

如果您尝试 link 到每个类别页面,您可以像这样使用 get_term_link()

$category_page_link = get_term_link($term);

<h2><?php echo $term->name; ?><a href="<?php echo esc_url( $category_page_link ); ?>">see all</a></h2>

以上内容 第二个 foreach 循环中。

编辑:

这是完整的代码,我把函数get_term_link()放在了它应该在的地方。此外,您在使用 opening/closing html 标签时遇到了一些问题:

<?php get_header(); ?>

<?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 ) );
?>
<section class="recipe-wrap">

<?php foreach ( $taxonomies as $taxonomy ) :

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

    foreach( $terms as $term ) :
        $term_page_link = get_term_link($term); ?>

        <div class="recipe-category owl-carousel-slide">
            <div class="row">
                <h2><?php echo $term->name; ?><a href="<?php echo esc_url ( $term_page_link ); ?>">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; ?>
                    <?php endif; ?>

                </div><!-- .owl-theme -->
            </div><!-- .row -->
        </div><!-- owl-carousel-slide -->

    <?php endforeach; // terms loop ?>

<?php endforeach; // taxonomies loop ?>

</section><!-- /recipe -->

<?php get_footer(); ?>

希望对您有所帮助。