首页 文章

使用PHP代码的Wordpress Woocommerce建议

提问于
浏览
3

我正在使用woo commerece插件,我想在每个产品的 Headers 下有一个子 Headers . 样式和格式已排序,但我希望在子 Headers 部分显示特定的类别 . 我已经设法显示所有类别,但我想将其缩小到只有一个父类别下的类别 . 下面是我正在使用的代码,任何人都可以建议我如何实现在父类别下选择的任何子类别 . 谢谢

<?php
/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>

<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Artist:', 'Artist:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); ?>

这是出来的:

数组([0] => stdClass对象([term_id] => 59 [名称] =>多彩[slug] =>多彩[term_group] => 0 [term_taxonomy_id] => 59 [分类法] => product_cat [描述] => [parent] => 115 [count] => 21 [filter] => raw)[1] => stdClass对象([term_id] => 96 [name] => Karen Grant [slug] => karen-grant [term_group] => 0 [term_taxonomy_id] => 96 [taxonomy] => product_cat [description] => [parent] => 90 [count] => 5 [filter] => raw)[2] => stdClass对象( [term_id] => 69 [name] =>风景[slug] =>风景[term_group] => 0 [term_taxonomy_id] => 69 [分类] => product_cat [描述] => [父] => 115 [数] => 35 [filter] => raw)[3] => stdClass对象([term_id] => 71 [name] => Nature [slug] => nature [term_group] => 0 [term_taxonomy_id] => 71 [分类] => product_cat [description] => [parent] => 115 [count] => 20 [filter] => raw))

<?php

/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>


<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php 


    global $post, $product;

    $cat_array = array();
    $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

    foreach($term_list as $cat_list)
    {

        array_push($cat_array, $cat_list->term_id);

    }

    $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

   $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

   $final_result = array_intersect($cat_array,$termchildren); print_r($final_result);

    $new_cat_id = $final_result[0];

    $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

    $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

    $name = $term->name; //Store it into an varialbe

    echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>

1 回答

  • 4

    试试这个 :

    <?php 
    
        global $post, $product;
    
        $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
    
        $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
    
        $cat_url = get_term_link ($cat_id, 'product_cat'); //get link of parent ID
    
        $term = get_term( $cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
    
        $name = $term->name; //Store it into an varialbe
    
        echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
    
    ?>
    

    Remember :

    WooCommerce 中,产品类别不是 normal categories ,它们是专为 products 创建的 custom taxonomy ,仅标记为 "Categories" .

    如果您有任何疑问,请告诉我 .

    Updated:

    <?php 
    
            global $post, $product;
    
            $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
    
            $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
    
           $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1
    
            $new_cat_id = $termchildren[2];
    
            $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID
    
            $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
    
            $name = $term->name; //Store it into an varialbe
    
            echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
    
        ?>
    

    New Updated(02 January 2015)

    <?php 
    
            global $post, $product;
    
            $cat_array = array();
            $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
    
            foreach($term_list as $cat_list)
            {
    
                array_push($cat_array, $cat_list->term_id);
    
            }
    
            $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
    
           $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1
    
           $final_result = array_intersect($cat_array,$termchildren);
    
           $final_cat_result = array_values($final_result);
    
            $new_cat_id = $final_cat_result[0];
    
            $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID
    
            $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
    
            $name = $term->name; //Store it into an varialbe
    
            echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
        ?>
    

    Line : 1 $post$product 都是 global variables .So在术语中使用它在其他文件中,我们需要在使用它之前在我们的文件中添加它 .

    Line : 2 一个 blank array 存储当前产品的所有类别** . 我们将来会使用它 .

    Line : 3 wp_get_post_terms用于 retrieve the terms of the post (对于woocommerce,它的产品类别) . 现在我们有一个包含 ID, name 等术语的所有细节的数组

    Line : 4 这是循环通过上面生成的数组 . 我们将遍历数组并查找 term_id . 我们将使用array_push来存储所有的术语ID,并且为了存储我们将使用来自第2行的空白数组 . 所以现在我们有一个数组 term_id

    Line : 9 现在我们将使用get_term_children来检索 Artist 的子项,因为我们知道艺术家 term ID 及其固定 . 它将给出 array 作为输出 .

    Line : 10 array_intersect用于匹配两个数组并仅提取匹配值 . (基本上我们正在查看当前产品类别和所有艺术家类别以仅取出匹配类别) .

    Line : 11 array_values对于重新索引数组非常有用 . (通过添加这一行,我们可以解决即将发生的错误:))

    Line : 12 现在我们有一个只有 one 值的数组,这是艺术家的 term ID . (就是这样 . 现在你只需从该术语ID中获取艺术家的名字和链接)

    Line : 13 获取艺术家的链接 .

    Line : 15 从第14行生成的数组中获取艺术家的名称,并将其存储在变量中 .

    Line : 16 打印所需的东西,我们完成了!

相关问题