草庐IT

php - 带有 PHP 代码的 Wordpress Woocommerce 建议

coder 2024-05-03 原文

我正在使用 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/

有关php - 带有 PHP 代码的 Wordpress Woocommerce 建议的更多相关文章

  1. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  2. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  3. ruby-on-rails - 浏览 Ruby 源代码 - 2

    我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru

  4. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  5. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  6. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  7. 程序员如何提高代码能力? - 2

    前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源

  8. 7个大一C语言必学的程序 / C语言经典代码大全 - 2

    嗨~大家好,这里是可莉!今天给大家带来的是7个C语言的经典基础代码~那一起往下看下去把【程序一】打印100到200之间的素数#includeintmain(){ inti; for(i=100;i 【程序二】输出乘法口诀表#includeintmain(){inti;for(i=1;i 【程序三】判断1000年---2000年之间的闰年#includeintmain(){intyear;for(year=1000;year 【程序四】给定两个整形变量的值,将两个值的内容进行交换。这里提供两种方法来进行交换,第一种为创建临时变量来进行交换,第二种是不创建临时变量而直接进行交换。1.创建临时变量来

  9. git使用常见问题(提交代码,合并冲突) - 2

    文章目录git常用命令(简介,详细参数往下看)Git提交代码步骤gitpullgitstatusgitaddgitcommitgitpushgit代码冲突合并问题方法一:放弃本地代码方法二:合并代码常用命令以及详细参数gitadd将文件添加到仓库:gitdiff比较文件异同gitlog查看历史记录gitreset代码回滚版本库相关操作远程仓库相关操作分支相关操作创建分支查看分支:gitbranch合并分支:gitmerge删除分支:gitbranch-ddev查看分支合并图:gitlog–graph–pretty=oneline–abbrev-commit撤消某次提交git用户名密码相关配置g

  10. HBase Region 简介和建议数量&大小 - 2

    Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile

随机推荐