首页 > 解决方案 > 反向 Woocommerce 产品订单

问题描述

第一次在这里发帖,所以不要介意菜鸟。我是一名网络和 Wordpress 设计师,不了解 php,但通常可以通过 - 这可能是一个简单的解决方案(甚至可能不需要 php),但它真的让我很难过。

我所做的更改(我相信来自自定义页面的产品订单)弄乱了我正在处理的网站主页上的产品订单。无论我尝试什么设置,它仍然以相同的顺序出现,当我们希望它最后显示时,保险杠贴纸是首选。

该订单似乎确实适用于“商店”页面,而不是首页。

作为最后的手段,我认为必须有一种方法至少可以反转 php 查询以显示它们向后列出......但必须有更好的方法

无论如何,这是代码


                    <div class="content--offset">


                        <?php

                        $args = array( 'post_type' => 'product');
                        $loop = new WP_Query( $args );

                        while ( $loop->have_posts() ) : $loop->the_post(); ?>
                        <?php $price = get_post_meta( get_the_ID(), '_price', true ); ?>
                        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );?>

                                <div class="content__item product" style="--aspect-ratio: 5/5;">
                                        <div class="content__item-imgwrap">
                                            <a href="<?php echo get_permalink( $loop->post->ID ) ?>"><h5><?php the_title(); ?><br><span><?php echo wc_price( $price ); ?></span></h5></a>
                                            <div class="content__item-img" style="background-image: url('<?php  echo $image[0]; ?>');"></div></div>
                                        <a href="product.html"><h5 class="content__item-title "></h5>
                                    </a>
                                </div>

                        <?php endwhile; wp_reset_query();?>


                    </div>

非常感谢任何帮助!谢谢!

标签: phpwordpresswoocommerce

解决方案


这在https://wordpress.stackexchange.com/上可能会更好。只为未来:)

您可以尝试在数组中添加'order' => 'DESC'参数吗?$args

像这样:

$args = array(
  'post_type' => 'product',
  'order' => 'DESC'
);

如果您想在查询后反转产品数组,另请参阅 PHP 的array_reverse方法。$loop


推荐阅读