在我的 Woocommerce 網站上,我有 2 個不同的產品屬性(藝術家和型別)。我想在包含這些屬性之一及其值的屬性存檔頁面上執行一些代碼。例如:
在 site.com/artist/rembrandt/ 上顯示倫勃朗 (pa_rembrandt) 制造的所有產品,我想添加文字“請看倫勃朗的所有繪畫”等。這是我迄今為止嘗試過的:
<?php add_action( 'woocommerce_archive_description', 'artist_description' );
function artist_description() {
if (is_product_taxonomy() && is_tax(pa_artist)) {
echo "Please see all paintings made by";
echo is_tax(pa_artist));
echo ".";
}
} ?>
但這顯然行不通。當我洗掉&& is_tax(pa_artist)
代碼時,確實會顯示“請查看所有繪畫作品”的文字。但沒有實際的畫家,并出現在每個檔案頁面上。誰能幫我?提前致謝。
uj5u.com熱心網友回復:
add_action('woocommerce_archive_description', 'artist_description');
function artist_description() {
if (check_is_wc_attribute('Artist')) {
echo "Please see all paintings made by";
}
}
function check_is_wc_attribute($attribute_name) {
/**
* Attributes are proper taxonomies, therefore first thing is
* to check if we are on a taxonomy page using the is_tax().
* Also, a further check if the taxonomy_is_product_attribute
* function exists is necessary, in order to ensure that this
* function does not produce fatal errors when the WooCommerce
* is not activated
*/
if (is_tax() && function_exists('taxonomy_is_product_attribute')) {
return taxonomy_is_product_attribute($attribute_name);
}
return false;
}
uj5u.com熱心網友回復:
試試下面的代碼。它將在產品標題后的產品網格中的每個存檔頁面/串列頁面上添加文本。此文本將顯示在您已添加此屬性的特定產品上。
function artist_description() {
global $product;
$artist_val = $product->get_attribute('pa_artist');
if ($artist_val) {
echo "Please see all paintings made by ";
echo $artist_val;
echo ".";
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'artist_description' );
uj5u.com熱心網友回復:
我通過使用(和剝離)標題并將代碼更改為此找到了解決方案:
function artist_description() {
if (is_product_taxonomy() & is_tax('pa_artist')) {
echo "All paintings made by " ;
$artist = sprintf(__('%1$s'), single_term_title('', false));
echo $artist;
echo ". </br>";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466199.html
標籤:WordPress woocommerce 属性 产品 分类
上一篇:回聲時逃跑