使用 PHP 代码的 WordPress Woocommerce 建议

2024-02-04

我正在使用 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>' ); ?>

结果是这样的:

数组 ( [0] => stdClass 对象 ( [term_id] => 59 [name] => 多彩 [slug] => 多彩 [term_group] => 0 [term_taxonomy_id] => 59 [分类] => 产品目录 [描述] => [父] => 115 [计数] => 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] => [父] => 90 [计数] => 5 [过滤器] => 原始 ) [2] => stdClass 对象 ( [term_id] => 69 [name] => 风景 [slug] => 风景 [term_group] => 0 [term_taxonomy_id] => 69 [分类] => Product_cat [描述] => [父] => 115 [计数] => 35 [过滤器] => 原始 ) [3] => stdClass 对象 ( [term_id] => 71 [名称] => 自然 [slug] => 自然 [term_group] => 0 [term_taxonomy_id] => 71 [taxonomy] => Product_cat [描述] => [父] => 115 [计数] => 20 [过滤器] => 原始 ) )

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

?>

记住 :

In WooCommerce,产品类别不正常类别,他们是一个自定义分类法专门为products它只是标记为「类别」.

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

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

    ?>

新更新(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 and $product两者都是全局变量因此,为了在其他文件中使用它,我们需要在使用它之前将其添加到我们的文件中。

Line : 2 One 空白数组存储当前产品的所有类别**。我们将来会使用它。

Line : 3 wp_get_post_terms http://codex.wordpress.org/Function_Reference/wp_get_post_terms#Examples习惯于检索帖子的条款(对于 woocommerce 来说,它是产品类别)。所以现在我们有一个数组,其中包含术语的所有详细信息ID, name etc etc

Line : 4它用于循环遍历上面生成的数组。我们将循环遍历一个数组并查找term_id.我们将使用array_push http://php.net/manual/en/function.array-push.php为了存储所有术语 ID,为了存储,我们将使用第 2 行中的空白数组。所以现在我们有一个数组term_id.

Line : 9现在我们将使用get_term_children http://codex.wordpress.org/Function_Reference/get_term_children#Examples检索子项Artist据我们所知这位艺术家term ID并且它是固定的。它将给出一个array作为输出。

线路:10 array_intersect http://php.net/manual/en/function.array-intersect.php对于匹配两个数组并仅取出匹配的值非常有用。(基本上我们正在查看当前的产品类别和所有艺术家类别以仅取出匹配的类别)。

线路:11 array_values http://php.net/manual/en/function.array-values.php对于重新索引数组很有用。(通过添加这一行,我们解决了即将出现的错误:))

线路:12现在我们有一个数组,其中只有one艺术家的价值term ID(就是这样。现在您只需要从该术语 ID 中获取艺术家的姓名和链接)

线路:13获取艺术家的链接。

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

线路:16打印所需的内容,我们就完成了!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 PHP 代码的 WordPress Woocommerce 建议 的相关文章

随机推荐

  • android 从 JavascriptInterface 启动Activity

    简单的一般问题 Webview 连接到我的 JavascriptInterface 类 并且它肯定是有用的 但是 因为 JavascriptInterface 不扩展 Activity 所以我似乎无法使用 startActivity int
  • 锦标赛分组放置算法

    给定对手种子列表 例如种子 1 到 16 我正在尝试编写一种算法 该算法将导致头号种子在该轮中对阵最低的种子 第二名种子对阵第二低的种子 依此类推 将 1 和 16 2 和 15 等分组为 比赛 相当容易 但我还需要确保较高的种子将在后续回
  • 使用C#或Powershell扫描所有可用的无线网络并连接到特定的SSID

    我正在尝试编写一个脚本来扫描所有可用的无线网络并连接到特定网络 SSID 有人已经为此编写了示例代码吗 由于某些限制 我无法安装第三方软件 托管 wifi api 查看这篇相关文章 在 C 中管理无线网络连接 https stackover
  • 未捕获的类型错误:无法解析模块说明符“firebase/app”。相对引用必须以“/”、“./”或“../”开头

    我遵循了有关 WebRTC 视频聊天的 YouTube 教程 因此我尝试编写它 在 localhost 中它可以工作 但是当我将其上传到 firebase 托管时 它就会出现此错误 我能做些什么 我是网络开发新手 所以请耐心等待 主要 ht
  • 如何从字符串中删除0

    我正在看函数trim但不幸的是 这并没有删除 0 我该如何将其添加到其中 我应该使用str replace 编辑 我要修改的字符串是一个消息编号 如下所示 00023460 功能ltrim 00023460 0 正是我需要的 显然我不想使用
  • 在 Python 中模拟导入模块

    我正在尝试对使用导入的外部对象的函数实施单元测试 例如助手 py is import os import pylons def some func arg var1 os path exist var2 os path getmtime v
  • 无法使用 OpenCV 从辅助网络摄像头的 VideoCapture 读取帧

    Code 与主网络摄像头 设备 0 完美配合的简单示例 VideoCapture cap 0 if cap isOpened std cout lt lt Unable to read stream from specified devic
  • 为什么我在 raw_input 期间无法捕获 KeyboardInterrupt?

    这是一个测试用例 try targ raw input Please enter target except KeyboardInterrupt print Cancelled print targ 当我按 ctrl c 时 我的输出如下
  • SqlAlchemy - 按关系属性过滤

    我对 SQLAlchemy 没有太多经验 但我遇到了一个无法解决的问题 我尝试搜索并尝试了很多代码 这是我的课程 简化为最重要的代码 class Patient Base tablename patients id Column Integ
  • 在 Bolts 中,如何使用 continueWith() 和 continueWithTask()?

    除了同步与异步之外 它们文档中的差异也让我感到困惑 他们的例子github页面 https github com BoltsFramework Bolts Android chaining tasks together看起来仍然是同步调用的
  • Netbeans - 从数据库生成实体类

    我使用的是 netbeans IDE 7 1 我正在尝试从数据库 sql server 生成实体类 我能够设置与此远程数据源的连接 但在数据库向导的新实体类中 表没有显示 并且在底部显示 选择至少一个表 我可以执行查询并浏览 netbean
  • 如何在Altera Quartus中生成.rbf文件?

    什么是 rbf 文件以及如何在 Windows 上从 Quartus 输出文件 sof 生成它们 An RBF is a 原始二进制文件例如 它代表原始数据 这些数据将被加载到闪存中 以便在上电时初始化 FPGA A SOF is an S
  • 获取数据访问层内的数据库上下文

    我在尝试解决 EF Core 方面的一些问题 我使用 MVC Core 应用程序中的启动代码来初始化数据库上下文 这是我的数据库上下文 public class AccountsDBContext DbContext public Acco
  • 将 UTC java.sql.Time 转换为具有正确 DST 的 java.time.localtime

    我在将从数据库获取的 java sql Time UTC 转换为 java time LocalTime GMT 1 DST 时遇到问题 总是缺少 DST 时间 因此 时间 03 00 仅转换为本地时间 04 00 而不是 05 00 Sa
  • 将 AES IV 存储在数据库中的密文前面是否安全?

    我想将 AES 加密数据存储在数据库字段中 将 AES IV 每行唯一 存储在密文前面是否安全 例如 IV 密文 两者都将以 Base64 进行编码 使用的Key不会存储在数据库中 这是安全的 初始化向量的目的是在生成的密码中插入一些随机性
  • 如何从控制台设置 parpool/matlabpool 中的最大工作人员数量?

    我知道如何使用 Matlab 中的并行首选项窗口更改最大工作人员数量 但我找不到任何有关如何从控制台 代码更改首选项的文档 特别是关于如何更改我可以的最大工作人员数量的文档在 for 循环中使用 任何帮助将不胜感激 你想要的parpool功
  • 在嵌套轨道表单上创建多对多关系

    我正在尝试同时创建组 用户和成员资格 多对多关系 人们可以在创建组时将用户添加到组中 然后我希望它能够路由到包含所有成员的组的视图 我可以将要创建的用户和当前用户的成员资格保存到数据库中 然而 我正在努力获取新创建的 User 对象的 id
  • Apache POI Excel 工作簿创建需要很长时间

    我注意到使用 Apache POI v3 10 例如 xlsx 文件的工作簿创建语句 Workbook wb WorkbookFactory create inputStream or Workbook wb new XSSFWorkboo
  • 命名空间别名的范围是什么?

    在函数定义内定义的 C 命名空间别名是否具有块 函数 文件或其他作用域 有效期 这是区块的有效期 例如 如果您按如下方式定义命名空间别名 则命名空间别名 abc 在外部无效 block namespace abc xyz abc test
  • 使用 PHP 代码的 WordPress Woocommerce 建议

    我正在使用 woo commerece 插件 我想在每个产品的标题下有一个子标题 样式和格式已排序 但我希望在子标题部分中显示特定的类别 我已经设法显示所有类别 但我想将其范围缩小到父类别下的一个类别 下面是我正在使用的代码 任何人都可以建