我在编辑带有钩子的Woocommerce模板时遇到了一些问题。本质上,我只是想改变product-image模板,而不是链接到上传的产品图像,它链接到产品帖子。
当前的product-image.php woocommerce模板具有
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>我不确定如何调整echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );以将%s更改为指向帖子的链接。
我使用的钩子是:
add_action('woocommerce_product_thumbnails', 'custom_links');
function custom_links() {
//code
}有没有人能帮我找到一些方向?
发布于 2014-01-15 18:57:16
您正在调用操作而不是筛选器。而且你打错电话了。
更改此设置:
add_action('woocommerce_product_thumbnails', 'custom_links');要这样做:
add_filter('woocommerce_single_product_image_html', 'custom_links', 10, 2);2表示函数的参数计数,您的custom_links()应该类似于:
function custom_links($link, $post_id) {
$pattern = '/(?<=href=")([^"]*)/';
$replacement = get_permalink($post->ID);
return preg_replace($pattern, $replacement, $link);
}根据需要处理$link变量,然后返回它。
https://stackoverflow.com/questions/21127301
复制相似问题