使用WordPress创建3个层次的关系

时间:2019-02-14 作者:Mintendo

我需要在Wordpress中创建这种类型的关系:

一个目录可以有多个产品,一个产品可以有多个目录,一个类别可以有多个产品,一个产品只能有一个类别,所以当我单击目录时,我需要查看与目录链接的所有产品类别。

E、 g:

因此,在前端,当我单击目录1时,我应该看到类别:-类别1-类别2

然后,当我单击类别1(目录1)时,我应该会看到产品:-产品1-产品2

如果单击目录2,我将看到类别:-类别1

然后,当我单击类别1(目录2)时,我应该会看到产品:-产品2

我如何在Wordpress+ACF上表达这些关系?

1 个回复
SO网友:Manyang

我不想尝试,但我希望这能奏效

function get_list_catalog_category_product(){
  //get all catalog
  $catalogs = get_terms( array(
    \'taxonomy\' => \'catalogs_term\',
    \'hide_empty\' => false
  ));

  if ( !empty($catalogs) ) :
  //make list catalog
  $output = \'<ul>\';

  foreach( $catalogs as $catalog ) {

      //write all catalog
      $output.= \'<li><a href="\'.get_term_link( $catalog ).\'">\'.$catalog->name.\'</a>\';

      //find all product related the catalog
      $args = array(
          \'post_type\' => \'product_post\',
          \'post_status\' => \'publish\',
          \'tax_query\' => array(
              array(
                  \'taxonomy\' => \'catalogs_term\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $catalog->term_id
              )
          )
      );
      $products = get_posts( $args );

      //make array of category related the catalog
      $all_category = array();

      //make array of product divided by category related the catalog
      $product_by_cat = array();

      if ( !empty($products) ){
        foreach( $products as $product ) {

          //write every product html
          $products_html =\'<li><a href="\'.get_permalink($product->ID).\'">\'.$product->post_title.\'</a></li>\';

          //find all category of this product
          $product_categories = wp_get_object_terms( $product->ID,  \'category\' );
          if ( ! empty( $product_categories ) ) {
            foreach($product_categories as $product_category) {

                //write the product html in related category if category never created this code will create or if has been create product html just append
                if(isset($product_by_cat[$product_category->term_id])){
                  $product_by_cat[$product_category->term_id] .= $products_html;
                }else{
                  $product_by_cat[$product_category->term_id] = $products_html;
                }

                //push the category as category related to catalog
                array_push($all_category,$product_category->term_id);
            }
          }

          //remove duplicate category if a product has more than 1 category
          $all_category = array_unique($all_category);
        }
      }

      if ( !empty($all_category) ){
        $output.= \'<ul>\';
          foreach( $all_category as $the_category ) {
            //write category html in catalog
            $output.=\'<li><a href="\'.get_term_link($the_category).\'">\'.get_term( $term_id, \'category\' )->name.\'</a></li>\';

            //write all product html to the category
            $output.=\'<ul>\'.$product_by_cat[$the_category].\'</ul>\';
          }
        $output.= \'</ul>\';
      }

      $output.= \'</li>\';
  }
  $output.=\'</ul>\';
  echo $output;
  endif;
}

相关推荐

Database problem

我的数据库有问题。首页工作正常,但当我想访问backoffice mysite时。com/wp admin我正在重定向到/wp admin/install。因此,我打开了phpmyadmin,看到有一个空数据库。。没有列,没有行,只有前端站点和所有页面工作正常吗?XD。发生了什么事?我有w3 total缓存和云flare。也许我的网站只因为我的数据库被缓存而工作?如何恢复数据库?请帮帮我,我没有备份。