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

Bre*_*ett 1 php wordpress twig woocommerce timber

我已经自定义了一些 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 );
Run Code Online (Sandbox Code Playgroud)

意见/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 class="before-products">
            {% do action('woocommerce_before_shop_loop') %}
        </div>

        <div class="products-wrap">
            <div class="products row flex">
                {% for post in products %}
                    <div class="product-holder col-xs-6 col-md-4">
                        {% do action('woocommerce_shop_loop') %}
                        {% include ["woo/partials/tease-product.twig"] %}
                    </div>
                {% endfor %}
            </div>
        </div>

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

        <div class="after-products">
            {% do action('woocommerce_after_shop_loop') %}
        </div>

    {% else %}

        <div class="no-products">
            {% do action('woocommerce_no_products_found') %}
        </div>

    {% endif %}

{% endblock  %}

{% block after_article %}
    {% do action('woocommerce_after_main_content') %}
    {{ parent() }}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

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 class="photo {% if not post.thumbnail %}placeholder{% endif %}">
        <a href="{{ post.link }}" class="link">
            {% if post.thumbnail %}
                <img src="{{ post.thumbnail.src|resize(600)|e('esc_url') }}" class="img-responsive" alt="{{ fn('esc_attr', title) }}" />
            {% else %}
                <img src="{{ fn('wc_placeholder_img_src') }}" class="img-responsive" alt="Awaiting product image" />
            {% endif %}
        </a>
    </div>

    <div class="details">

        <h3 class="entry-title">

            {% 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>
Run Code Online (Sandbox Code Playgroud)

问题区域是这一点:

<h3 class="entry-title">

    {% 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>
Run Code Online (Sandbox Code Playgroud)

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

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/">';
}
Run Code Online (Sandbox Code Playgroud)

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

Phi*_*hil 5

永久链接可通过全局产品变量访问,如您通过 {{ 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 ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
Run Code Online (Sandbox Code Playgroud)

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