WordPress的Woocommerce建議我與宇commerece插件工作,我想有一子每個產品的標題標題下。然而 樣式和格式進行排序,我想一個特定的類別涉及的子標題部分顯示。我已經成功地獲得儘可能顯示所有類別,但我想縮小這個下降到只有一類是父類。 下面是我使用的代碼,任何人都可以建議我怎麼能實現顯示父類別下選擇任何子類。 感謝有PHP代碼
<?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>";
?>
'
我沒有得到你,請你詳細說明一下嗎? –
我在我的網站上有一個產品,我想在產品標題下添加產品的藝術家。我已經這樣做了,但我想顯示藝術家的名字。每個藝術家也是一個類別,所以我想顯示鏈接到這個產品的類別,但我有其他類別鏈接到這個。我想只允許顯示藝術家的父類別,這將消除所有其他類別。父類別是藝術家,有沒有辦法說產品類別的藝術家,以便它不會僅顯示來自藝術家的所有類別的孩子。 – user3816812
我仍然沒有得到它:)但我得到的是,你只想在藝術家的標題下面有一個名字。所以如果是這種情況,我會建議你處理'custom field'並檢索它。 –