简体   繁体   English

使用产品类别过滤自定义帖子类型存档

[英]Filter custom post type archive with product category

I have a custom post type based on Woocommerce product taxonomy.我有一个基于 Woocommerce 产品分类的自定义帖子类型。 I would like to use these taxonomies to filter custom posts on the archive page.我想使用这些分类法来过滤存档页面上的自定义帖子。

I have read a lot of articles on this but could not achieve my goal.我已经阅读了很多关于此的文章,但无法实现我的目标。 So far I have created a new archive-mypost.php page and displayed all the customs posts and all the categories with the following code :到目前为止,我已经创建了一个新的 archive-mypost.php 页面,并使用以下代码显示了所有海关帖子和所有类别:

<div class="filter-custom-taxonomy">

     <?php
    $terms = get_terms( ['taxonomy' => 'product_cat'] );
    foreach ( $terms as $term ) : ?>
    <a href="?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>">
    <?php echo esc_html( $term->name ); ?>
    </a>
    <?php endforeach; ?>
    </div>
                    
    <div class="row">
        <div class="small-12 columns">
            <?php do_action( 'thb_archive_title' ); ?>
            <div class="row">
                <?php
                if ( have_posts() ) :
                    while ( have_posts() ) :
                        the_post();
                        get_template_part( 'inc/templates/post-styles/post-style1' );
                    endwhile;
                endif;
                ?>
            </div>
            <?php
            the_posts_pagination(
                array(
                    'prev_text' => '',
                    'next_text' => '',
                    'mid_size'  => 2,
                )
            );
            ?>
        </div>
    </div>

But the filter does not work, when I click on a categories nothing happens.但是过滤器不起作用,当我点击一个类别时没有任何反应。 Also I am still trying to figure out how to show only categories with results and hide the rest.此外,我仍在尝试弄清楚如何仅显示带有结果的类别并隐藏其余类别。

Thanks everyone !感谢大家 !

I have figured part of the solution This code works to display woocommerce categories on custom post archive page and filter results.我已经找到了解决方案的一部分此代码可用于在自定义帖子存档页面上显示 woocommerce 类别并过滤结果。

Only thing I do not know yet is to display only top categories first, then when the top category is selected, show subcategories我唯一不知道的是首先仅显示顶级类别,然后选择顶级类别时,显示子类别

<div class="filter-custom-taxonomy">
<?php
$terms = get_terms( ['taxonomy' => 'product_cat'] );
foreach ( $terms as $term ) : ?>
<a href="<?php echo home_url() ?>/tutos/?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>">
<?php echo esc_html( $term->name ); ?>
</a>
<?php endforeach; ?>
</div>
                
<div class="row">
    <div class="small-12 columns">
        <?php do_action( 'thb_archive_title' ); ?>
        <div class="row">
            <?php
            if ( have_posts() ) :
                while ( have_posts() ) :
                    the_post();
                    get_template_part( 'inc/templates/post-styles/post-style1' );
                endwhile;
            endif;
            ?>
        </div>
        <?php
        the_posts_pagination(
            array(
                'prev_text' => '',
                'next_text' => '',
                'mid_size'  => 2,
            )
        );
        ?>
    </div>
</div>

And in my function.php page :在我的 function.php 页面中:

function kbites_words_filter_archive( $query ) {
      if ( ! $query->is_main_query() )
      return $query;
      if ( is_admin() ) {
              return;
      }
      if ( is_post_type_archive('tutos') ) {
        if (isset($_GET['getby'])) {
              if ( 'cat' === $_GET['getby'] ) {
                          $taxquery = array(
                                  array(
                                          'taxonomy' => 'product_cat',
                                          'field' => 'slug',
                                          'terms' => $_GET['cat'],
                                  ),
                          );
                          $query->set( 'tax_query', $taxquery );
              }
      }
      $query->set( 'posts_per_page', 30 );
    }
      return $query;
}
add_action( 'pre_get_posts', 'kbites_words_filter_archive');

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

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