带有 PHP 代码的 Wordpress Woocommerce 建议 [英] Wordpress Woocommerce Advice with PHP code

查看:23
本文介绍了带有 PHP 代码的 Wordpress Woocommerce 建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with the woo commerece plugin and i'd like to have a sub heading under the title of each product. Style and format is sorted however i want a particular Category to show in the sub heading section. I've managed to get as far as showing all categories but i want to narrow this down to just one category that is under a parent category. Below is the code i am using, could anyone suggest how i could achieve showing any child category selected under a parent category. Thanks

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

<小时>

结果是这样的:


This is what came out:

Array ( [0] => stdClass Object ( [term_id] => 59 [name] => 多彩[slug] => 多彩 [term_group] => 0 [term_taxonomy_id] => 59[分类] => product_cat [描述] => [父] => 115 [计数] =>21 [过滤器] => 原始)[1] => stdClass 对象([term_id] => 96 [名称]=> Karen Grant [slug] => karen-grant [term_group] => 0 [term_taxonomy_id] => 96 [taxonomy] => product_cat [描述] =>[parent] => 90 [count] => 5 [filter] => raw ) [2] => stdClass Object ([term_id] => 69 [name] => Landscapes [slug] => Landscapes [term_group]=> 0 [term_taxonomy_id] => 69 [taxonomy] => product_cat [描述] => [parent] => 115 [count] => 35 [filter] => raw ) [3] => stdClass Object ([term_id]=> 71 [名称] => 自然 [蛞蝓] => 自然[term_group] => 0 [term_taxonomy_id] => 71 [taxonomy] => product_cat[描述] => [父] => 115 [计数] => 20 [过滤器] => 原始 ) )

Array ( [0] => stdClass Object ( [term_id] => 59 [name] => Colourful [slug] => colourful [term_group] => 0 [term_taxonomy_id] => 59 [taxonomy] => product_cat [description] => [parent] => 115 [count] => 21 [filter] => raw ) [1] => stdClass Object ( [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 Object ( [term_id] => 69 [name] => Landscapes [slug] => landscapes [term_group] => 0 [term_taxonomy_id] => 69 [taxonomy] => product_cat [description] => [parent] => 115 [count] => 35 [filter] => raw ) [3] => stdClass Object ( [term_id] => 71 [name] => Nature [slug] => nature [term_group] => 0 [term_taxonomy_id] => 71 [taxonomy] => 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>";
?>

'

推荐答案

试试这个:

<?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>";

?>

记住:

WooCommerce中,产品类​​别不是普通类别,而是专为产品创建的自定义分类这只是标记为类别".

In WooCommerce, products categories are not normal categories, they are a custom taxonomy created specifically for products which is just labeled as "Categories".

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

Let me know If you have any doubt.

更新:

    <?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>";

    ?>

新更新(2015 年 1 月 2 日)

    <?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 都是全局变量.所以就使用而言在其他文件中,我们需要在使用之前将其添加到我们的文件中.

Line : 1 $post and $product both are the global variables.So in term to use that in other files, we need to add that in our file before using it.

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

Line : 2 One blank array to store all the categories of the current product**.We will use it in a future.

第 3 行 wp_get_post_terms 用于检索帖子的术语(对于 woocommerce 其产品类别).所以现在我们有一个包含 ID, name 的术语的所有详细信息的数组等等等等

Line : 3 wp_get_post_terms is used to retrieve the terms of the post(for woocommerce its category for product).So now we have an array containing all the details of terms with ID, name etc etc

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

Line : 4 It is for the loop through above generated array.We will loop through an array and look for term_id.We will use array_push to store all the term ID and for storing we will use blank array from line 2.So now we have an array of term_id.

第 9 行现在我们将使用 get_term_children 检索Artist 的子项,因为我们知道艺术家term ID 及其固定值.它将给出一个数组作为输出.

Line : 9 Now we will use get_term_children to retrieve the children term of Artist as we know the artist term ID and its fixed.It will give an array as an output.

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

Line : 10 array_intersect is useful to match two array and fetch out only matching values.(Basically we are looking in to the current product category and all artist category to take out the matching category only).

第 11 行 array_values 对重新索引数组很有用.(通过添加这一行,我们解决了即将出现的错误 :) )

Line : 11 array_values is useful to re-index the array.(By adding this line we solve the error that was coming :) )

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

Line : 12 Now we have an array that is having only one value which is artist's term ID.(That's it.Now you need to fetch only name and link of the artist from that term ID)

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

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

Line : 15 Fetch name of the artist from an array generated in line 14 and store it in variable.

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

Line : 16 Print the needed thing and we are done !

这篇关于带有 PHP 代码的 Wordpress Woocommerce 建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆