我正在使用woo commerece插件,并且希望每个产品的标题下都有一个子标题.
样式和格式已排序,但是我希望特定类别显示在子标题部分.我已经设法显示所有类别,但我想将其缩小到父类别下的一个类别.
以下是我使用的代码,谁能建议我如何实现显示在父类别下选择的任何子类别.
谢谢
/**
* 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' ) );
?>
<?php the_title(); ?>
<?php echo $product->get_categories( ', ', '' . _n( 'Artist:', 'Artist:', $cat_count, 'woocommerce' ) . ' ', '.' ); ?>
结果是这样的:
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 ) )
/**
* 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' ) );
?>
<?php the_title(); ?>
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: ".$name."";
?>
‘
解决方法:
尝试这个 :
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: ".$name."";
?>
请记住:
在WooCommerce中,产品类别不是常规类别,它们是专门为产品创建的自定义分类法,仅将其标记为“类别”.
如果您有任何疑问,请告诉我.
更新:
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: ".$name."";
?>
新更新(2015年1月2日)
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: ".$name."";
?>
第1行:$post和$product都是全局变量,因此要在其他文件中使用它,我们需要在使用它之前在文件中添加它.
第2行:一个空白数组,用于存储当前产品的所有类别**.以后我们将使用它.
Line:3 wp_get_post_terms用于检索帖子的术语(对于woocommerce,其产品类别).因此,现在我们有了一个数组,其中包含带有ID,名称等的术语的所有详细信息
第4行:用于遍历上面生成的数组.我们将遍历一个数组并查找term_id.我们将使用array_push存储所有术语ID,并使用第2行中的空白数组进行存储. term_id的数组.
第9行:现在我们将使用get_term_children来检索Artist的子项,因为我们知道Artist项ID及其固定值,它将给出一个数组作为输出.
Line:10 array_intersect对于匹配两个数组并仅提取匹配值很有用(基本上,我们正在查看当前产品类别和所有艺术家类别,仅提取匹配类别).
Line:11 array_values对于重新索引数组很有用.(通过添加此行,我们解决了即将出现的错误:))
第12行:现在我们有了一个数组,该数组只有一个值是艺术家的术语ID(就是这样,现在您只需要从该术语ID中获取艺术家的名称和链接)
行:13获取艺术家的链接.
第15行:从第14行中生成的数组中获取艺术家的名称,并将其存储在变量中.
行:16打印所需的内容,我们完成了!
标签:wordpress,php
来源: https://codeday.me/bug/20191028/1955293.html