使用Twig和Timber时在WooCommerce循环中获取正确的产品链接

我已经自定义了一些 WooCommerce 模板,并且正在使用 Twig 模板系统和 Timber。

在类别页面上,我刚刚注意到产品图片都正确链接到相应的产品,但它们下面的标题链接到页面上的第一个产品。

两者之间的区别是自定义了带有照片的正确链接部分,而错误的链接标题是通过 WooCommerce 挂钩生成的。

这是我各自的文件:

存档-product.php

defined( 'ABSPATH' ) || exit;

$context            = Timber::get_context();
$context['shop-sidebar'] = Timber::get_widgets( 'shop-sidebar' );

$posts = Timber::get_posts();
$context['products'] = $posts;

$context['search_str'] = (get_search_query()) ? get_search_query() : '';

if ( is_product_category() ) {
    $queried_object = get_queried_object();
    $term_id = $queried_object->term_id;
    $context['category'] = get_term( $term_id, 'product_cat' );
    $context['title'] = single_term_title( '', false );
} elseif (is_shop()) {

    $shop_id = get_option( 'woocommerce_shop_page_id' );
    $post = new TimberPost($shop_id);

    $alt_title = (!empty($post->get_field('alt_page_title')['title'])) ? $post->get_field('alt_page_title')['title'] : '';
    $context['title'] = ($alt_title) ? $alt_title : $post->post_title;

}

Timber::render( 'views/woo/archive.twig', $context );

意见/woo/archive.twig

{% extends 'archive.twig' %} {# Base Archive File #}

{% block before_article %}
    {% do action('woocommerce_before_main_content') %}
{% endblock %}

{% block below_h1 %}
    {{ fn('woocommerce_result_count') }}
{% endblock %}

{% block intro_content %}
    {% do action('woocommerce_archive_description') %}
{% endblock %}

{% block primary_block %}

    {% if products|length > 0 %}

        <div>
            {% do action('woocommerce_before_shop_loop') %}
        </div>

        <div>
            <div>
                {% for post in products %}
                    <div>
                        {% do action('woocommerce_shop_loop') %}
                        {% include ["woo/partials/tease-product.twig"] %}
                    </div>
                {% endfor %}
            </div>
        </div>

        {% if fn('show_products_nav') %}
            <nav role="navigation">
                {{ fn('wp_paginate') }}
            </nav>
        {% endif %}

        <div>
            {% do action('woocommerce_after_shop_loop') %}
        </div>

    {% else %}

        <div>
            {% do action('woocommerce_no_products_found') %}
        </div>

    {% endif %}

{% endblock  %}

{% block after_article %}
    {% do action('woocommerce_after_main_content') %}
    {{ parent() }}
{% endblock %}

woo/partials/tease-product.twig

<article {{ fn('post_class', ['entry'] ) }}>

    {{ fn('timber_set_product', post) }}

    {% set title = (post.title) ? post.title : fn('the_title') %}

    <div>
        <a href="{{ post.link }}">
            {% if post.thumbnail %}
                <img src="{{ post.thumbnail.src|resize(600)|e('esc_url') }}" alt="{{ fn('esc_attr', title) }}" />
            {% else %}
                <img src="{{ fn('wc_placeholder_img_src') }}" alt="Awaiting product image" />
            {% endif %}
        </a>
    </div>

    <div>

        <h3>

            {% do action('woocommerce_before_shop_loop_item') %}
            {% do action('woocommerce_before_shop_loop_item_title') %}
                {{ title|e2 }}
            {% do action( 'woocommerce_after_shop_loop_item_title' ) %}
            {% do action( 'woocommerce_after_shop_loop_item' ) %}

        </h3>

    </div>

</article>

问题区域是这一点:

<h3>

    {% do action('woocommerce_before_shop_loop_item') %}
    {% do action('woocommerce_before_shop_loop_item_title') %}
        {{ title|e2 }}
    {% do action( 'woocommerce_after_shop_loop_item_title' ) %}
    {% do action( 'woocommerce_after_shop_loop_item' ) %}

</h3>

据我所知,我可以用这个修改锚标签:

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'myprefix_woocommerce_template_loop_product_link_open', 10 );

function myprefix_woocommerce_template_loop_product_link_open() {
    echo '<a href="https://www.google.com.au/">';
}

我只是不确定放置此代码的最佳位置以及如何将正确的永久链接传递给它。

回答

永久链接可通过全局产品变量访问,如您通过 {{ fn('timber_set_product', post) }}

将代码放在您的functions.php 中。

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'myprefix_woocommerce_template_loop_product_link_open', 10 );

function myprefix_woocommerce_template_loop_product_link_open() {
    global $product;
    $link = $product->get_permalink();
    echo '<a href="' . esc_url( $link ) . '">';
}

  • 好的,我注意到 `$product` 有正确的细节,所以我可以用 `$link = $product-&gt;get_permalink();` 来获取它:)

以上是使用Twig和Timber时在WooCommerce循环中获取正确的产品链接的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>