繁体   English   中英

Woocommerce “存档页面”产品标题及链接

[英]Woocommerce “archive page” product title with link

我一直专注于创建自定义 woocommerce 商店存档页面,而不使用标准的“产品列表”模块。 我正在使用 Oxygen 构建器,他们提供了一个“中继器”模块来构建产品列表,允许您根据需要在 div 中堆叠元素。

我面临一个问题。 我正在使用一些单一的产品页面元素来构建布局。 问题是单个产品标题“产品标题”模块没有像存档产品标题那样嵌入链接。

我一直在寻找一些代码,但没有得到我想要的。 请不要怪我,我已经做了很多研究,但我还没有找到解决方案。

这是我实际创建的 2 个代码。 我已经将两者叠加在一起,但不知道如何使其 100% 正确工作。 对于你们中的一些人来说,这个问题可能很明显!

实际上它用产品链接包装了产品标题,它在前端工作得很好,但我在下面还有一个带有“产品价格”模块的 div,它也用相同的链接包装......点击价格也会重定向到单个产品页面!

<a href="<?php echo get_the_permalink(); ?>"><?php _e('<?php');?></a>

<?php
global $product;

// If the global WC_product Object is not accessible
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}
echo $product->get_name();
?>

这是我发现的原始代码,用于显示重定向到产品页面的“查看更多”链接。

<a href="<?php echo get_the_permalink(); ?>"><?php _e('view more', 'woocommerce');?></a>

这是显示产品标题的代码,但它没有链接到单个产品页面......

<?php
global $product;

// If the global WC_product Object is not accessible
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}
echo $product->get_name();
?>

目标是有一个链接到单个产品页面的产品标题。

我想知道是否有人能够找到解决方案?

您可以将产品名称包装到锚标签。 检查下面的代码。

CSS

a.yourclassname:hover {
  color: red;
}

PHP

<?php
global $product;

// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}
echo '<a href="'.get_the_permalink().'" class="yourclassname">'.$product->get_name().'</a>';
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM