- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 woo commerece 插件,我希望在每个产品的标题下有一个子标题。样式和格式已排序,但我希望在子标题部分显示特定类别。我已经设法显示所有类别,但我想将其缩小到父类别下的一个类别。下面是我正在使用的代码,任何人都可以建议我如何实现显示在父类别下选择的任何子类别。谢谢
<?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>' ); ?>
结果是这样的:
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
中,产品类别不是普通类别,它们是专门为产品创建的自定义分类法这只是标记为“类别”。
如果您有任何疑问,请告诉我。
更新:
<?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 : 2 一个空白数组,存放当前商品的所有分类**,以后会用到。
第 3 行 wp_get_post_terms
用于检索帖子的条款(对于 woocommerce 其产品类别)。所以现在我们有一个包含所有条款详细信息的数组,包括 ID、name
等
第 4 行 用于遍历上面生成的数组。我们将遍历一个数组并查找 term_id
。我们将使用 array_push
为了存储所有术语 ID 和存储,我们将使用第 2 行中的空白数组。所以现在我们有一个 term_id
数组。
第 9 行 现在我们将使用 get_term_children
检索 Artist 的子术语,因为我们知道艺术家 term ID
及其固定值。它将提供一个数组 作为输出。
第 10 行 array_intersect
用于匹配两个数组并仅获取匹配值。(基本上我们正在查看当前产品类别和所有艺术家类别以仅取出匹配类别)。
第 11 行 array_values
对重新索引数组很有用。(通过添加这一行,我们解决了即将到来的错误:))
第 12 行 现在我们有一个数组,它只有 一个 值,即艺术家的 term ID
。(就是这样。现在你只需要从该术语 ID 中获取艺术家的姓名和链接)
第 13 行获取艺术家的链接。
第 15 行 从第 14 行生成的数组中获取艺术家的姓名并将其存储在变量中。
第 16 行打印所需的内容,我们就完成了!
关于php - 带有 PHP 代码的 Wordpress Woocommerce 建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712678/
我是一个相对较新的程序员; CS 学士学位,大学毕业大约 2 年,主要使用 C# 中的 .NET。我对 SQL 交互/脚本编写相当流利,并且对 ASP.NET 做了一些工作(主要是维护现有站点)。 我
我计划开发一个简单的解决方案,使我能够即时执行非常基本的视频流分析。我以前从未做过类似的事情,因此这是一个非常笼统和开放的问题。主要重点是检查流是否正常运行,例如 - 卡住帧、黑屏以及音频是否存在。同
我正在考虑重组一个大型 Maven 项目...... 我们当前结构的基本概述: build [MVN plugins, third party dependency management]:5.1
我需要有关附加查询的建议。该查询执行了一个多小时,并根据解释计划进行了全表扫描。我对查询调优还很陌生,希望得到一些建议。 首先,为什么我要进行全表扫描,即使我使用的所有列都在其上创建了索引。 其次,有
我正在做一个项目,我需要在 4 个模型之间创建三个多对多关系。这是它的过程: 常见问题类别可以有许多常见问题子类别,反之亦然。 常见问题组可以有许多常见问题的子类别,反之亦然。 常见问题可以有许多常见
对于代码大小比语音质量更重要的 PIC 和/或 ARM 嵌入式系统,是否有任何易于使用的免费或廉价的语音合成库?现在似乎 1 meg 的封装被认为是“紧凑的”,但很多微 Controller 都比它小
我们正在使用 Solr 建议器功能进行 businessName 查找。当用户输入查询以及匹配的名称时,我们希望 solr 发送来自个人资料的其他属性,如 id、地址、城市、州、国家等字段。 我尝试使
我正在构建一个用户界面。我的计划将包括 4 个主要部分: 1) 顶部菜单 - TMainMenu。一个窗口的顶部 2) 主菜单 - TTreeView。一个窗口的左边。 TreeView的每一项=对应
我的公司需要一个任务管理系统来处理从“为X购买一台计算机”到“将一个人转移到另一个国家”这样简单的场景。简单的场景是由一个人处理的单个任务,而更大的任务可以分解为在工作流程中委派给多个人的多个子任务。
MarkLogic 服务器的林大小与实际内存的建议比率是多少?例如,我目前有一个 190GB 的数据库,并且该数据库随着时间的推移而不断增长。由于数据库会不断增长,我最终需要对该数据库进行集群。因此,
去年我收到了一个礼物,它是一个索尼 CMT700Ni 音频站,支持 wifi。它还具有类似于广播的功能,称为“PartyStreaming”。我目前正在挖掘内部,探索它,所以也许我可以结束拥有自己的“
有没有我可以阅读的研究论文/书籍可以告诉我针对手头的问题哪种特征选择算法最有效。 我试图简单地将 Twitter 消息识别为 pos/neg(首先)。我从基于频率的特征选择开始(从 NLTK 书开始)
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在浏览 stackoverflow 以查找有关使用 jUnit 进行测试的常见建议,但仍然有几个问题。我知道,如果要测试的方法很复杂,最好的方法是将其分成小的单独部分并测试每个部分。但问题是 -
我有一个方法如下 public List> categorize(List customClass){ List> returnValue = new ArrayList<>();
我的问题是,当按照下面的程序合并时,在最佳实践场景中,“将分支折叠回主干”程序的最后一步是正确的方法吗? 我已经使用 svn 很多年了。在我的个人项目中,我总是毫不犹豫地在主干上愉快地进行修改,并且在
我读过 UINavigationController当您想从 n 个屏幕跳转到第一个屏幕时,这是最佳选择。这样做需要以下代码: NSMutableArray *array=[[NSMutableArr
我有一个文件输入类。它在构造函数中有一个字符串参数来加载提供的文件名。但是,如果文件不存在,它就会退出。如果文件不存在,我希望它输出一条消息 - 但不确定如何...... 这是类(class): pu
我希望创建一个“您访问过的国家/地区” map - 就像您可能在 Facebook、TravelAdvisor 和诸如此类的网站上看到的那样。 我尝试过不同的闪光灯套件,但它们并不像我希望的那样先进。
我需要一些关于如何处理我想用 Perl 编写的脚本的建议。基本上我有一个看起来像这样的文件: id: 1 Relationship: "" name: shelby pet: 1
我是一名优秀的程序员,十分优秀!