Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用钩子覆盖模板_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 用钩子覆盖模板

Php 用钩子覆盖模板,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在编辑带有挂钩的Woocommerce模板时遇到了一些问题。本质上,我只想更改产品图片模板,这样它就可以链接到产品帖子,而不是链接到上传的产品图片 当前的product image.phpwoocommerce模板 global $post, $woocommerce, $product; ?> <div class="images"> <?php if ( has_post_thumbnail() ) { $image_title

我在编辑带有挂钩的Woocommerce模板时遇到了一些问题。本质上,我只想更改
产品图片
模板,这样它就可以链接到产品帖子,而不是链接到上传的产品图片

当前的
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>

有人能帮我找到一些方向吗?

你说的是行动而不是过滤。你打错电话了

更改此项:

add_action('woocommerce_product_thumbnails', 'custom_links');
为此:

add_filter('woocommerce_single_product_image_html', 'custom_links', 10, 2);
2
表示函数的参数计数,您的
自定义链接()
应该类似于:

function custom_links($link, $post_id) {
    $pattern = '/(?<=href=")([^"]*)/';
    $replacement = get_permalink($post->ID);
    return preg_replace($pattern, $replacement, $link);
}
函数自定义链接($link,$post\u id){

$pattern='/(?感谢您的响应!函数似乎正在工作,但是当我更改
$link
的值时,它会删除图片,您知道如何更改href并保持src不变吗?@Chris这是因为$link变量包含类似“”的html。请使用preg_replace()(或任何其他字符串函数)仅修改url或仅重新生成整个字符串。您有产品ID。只需获取图像和url并重做字符串。当我尝试替换$link字符串时,我遇到了致命错误。即,使其仅显示图像,而不尝试将其链接到post
函数自定义链接($link,$post_ID){$link=“”;return$link;}
我想我缺少一条规则。@克里斯,这是因为你的双引号错误,并且你没有$image变量。你不能访问模板中的相同变量。
函数自定义链接($link,$post_id){$pattern='/(?
function custom_links($link, $post_id) {
    $pattern = '/(?<=href=")([^"]*)/';
    $replacement = get_permalink($post->ID);
    return preg_replace($pattern, $replacement, $link);
}