【问题标题】:How to Show Related Products by Category in WordPress如何在 WordPress 中按类别显示相关产品
【发布时间】:2018-07-26 12:54:28
【问题描述】:

如何在 WordPress 中按类别显示相关产品?

产品分类:

  • 活动地板
  • 辅助产品
  • 可耐福地板
  • 再生板

例如当前页面是活动地板仅显示辅助产品、可耐福地板、相关产品中的回收板。

我在 single-products.php 中添加了代码,见下文:

$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_category', $cat_args );
if( !empty($product_categories) ){
    echo '<div class="container">';
    echo '<div class="row">';
    foreach ($product_categories as $key => $category) {
        $description = $category->description;
        echo '<div class="col-lg-6">';
        echo '<div class="card">';
        echo '<a href="'.get_term_link($category).'" >';
        $image = get_field('product_category', $category );
        if($image) {
            echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
        } else {
            echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
        }
        echo '<div class="card-content">';
        echo '<h3>' . $category->name . '</h3>';
        echo '<p>';
        echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
        echo '</p>';
        echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($category).'" >Discover more</button>';
        echo '</div>';
        echo '</a>';
        echo '</div>';
        echo '</div>';
    }
    echo '</div>';
    echo '</div>';
}
else {
// no posts found
   echo wpautop( 'Sorry, no products were found' );
}

【问题讨论】:

  • @SudharshanNair 我之前尝试过,但不适用于 single-products.php
  • 该解决方案不基于页面。它应该工作
  • 好的,如何在single-products.php中添加代码?
  • 在 function.php 文件中创建该函数并在 single-products.php 中使用它

标签: php wordpress categories


【解决方案1】:

首先,获取与当前产品关联的类别的术语:

$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );

通过get_the_id()函数获取当前商品的id

其次,获取具有相同条款的其他产品(从结果中排除当前产品):

$products = get_posts(array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'numberposts' => -1,
    'post__not_in' => array(get_the_id()),
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $terms,
            'include_children' => false
        )
    )
));

完整的代码是:

$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
$products = get_posts(array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'numberposts' => -1,
    'post__not_in' => array(get_the_id()),
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $terms,
            'include_children' => false
        )
    )
));

【讨论】:

  • 谢谢,我的意思是当他们点击 single-products.php 中的任何产品类别时,我想自动隐藏当前产品知道我的意思
  • 看截图:bubblei.co.uk/FGTvz5.png,所以当点击可耐福地板时,可耐福地板页面应该有2个产品(活动地板,辅助产品),如何自动当前页面(如删除当前产品)在 single-products.php 中只有一个文件?
  • 我把上面的代码贴上去,你可以编辑一下:stackoverflow.com/a/51542355/10025598 :)
  • 请看,在我的代码中,我只得到了条款的 id: $terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') ) ;
  • 请看:这个只显示当前产品,不显示其他3个产品,我做错了吗?
【解决方案2】:

看,我得到了一个只有术语 id 的数组:

$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );

所以,你应该编辑你的 sn-p 代码:

foreach ($terms as $key => $term) {

如下:

foreach ($terms as $key => $term_id) {
    $term = get_term($term_id);

顺便说一句,在您的代码中,您没有将 $products 变量与相关产品数组一起使用,这是您最初的问题。

【讨论】:

    【解决方案3】:

    @MikeD这个只显示当前产品不显示其他3个产品,我做错了吗?

    `<?php
                    $orderby = 'name';
                    $order = 'random';
                    $hide_empty = true ;
                    $cat_args = array(
                        'orderby'    => $orderby,
                        'order'      => $order,
                        'hide_empty' => $hide_empty,
                        );
                    $terms = wp_get_post_terms( get_the_id(), 'product_category', array('fields' => 'ids') );
                    echo '<div class="container">';
                    echo '<div class="row">';
                        foreach ($terms as $key => $term) {
                            $term = get_term($term_id);
                            echo '<a href="' .get_term_link($term). '" ></a>';
                            echo '<div class="col-lg-6">';
                            echo '<div class="card">';
                            echo '<a href="'.get_term_link($term).'" >';
                            $image = get_field('product_category', $term );
                            if($image) {
                                echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
                            } else {
                                echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
                            }
                            echo '<div class="card-content">';
                            echo '<h3>' . $term->name . '</h3>';
                            //echo $category->name;
                            //echo '<p>' . $category->description . '</p>';
                            echo '<p>';
                            echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
                            echo '</p>';
                            //echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>';
                            echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($term).'" >Discover more</button>';
                            echo '</div>';
                            echo '</a>';
                            echo '</div>';
                            echo '</div>';
                        }
                        echo '</div>';
                        echo '</div>';
                    ?>`
    

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多