Php 更改特定类别存档的默认排序顺序
在Woocommerce中,我试图更改“新建”产品类别归档页面的默认排序顺序(按日期顺序递减),以便首先列出最新添加的内容 我尝试了两个不同的代码片段。两者都会更改默认顺序,但会首先显示最旧的产品 在这两个代码段中,我都将ASC更改为DESC,并且都没有提供对排序顺序的更改 我是一个非常新的编码,并感谢任何帮助我的地方出错 第一次尝试:Php 更改特定类别存档的默认排序顺序,php,wordpress,sorting,date,woocommerce,Php,Wordpress,Sorting,Date,Woocommerce,在Woocommerce中,我试图更改“新建”产品类别归档页面的默认排序顺序(按日期顺序递减),以便首先列出最新添加的内容 我尝试了两个不同的代码片段。两者都会更改默认顺序,但会首先显示最旧的产品 在这两个代码段中,我都将ASC更改为DESC,并且都没有提供对排序顺序的更改 我是一个非常新的编码,并感谢任何帮助我的地方出错 第一次尝试: add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_order
add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
function custom_default_catalog_orderby() {
$product_category = array( 'new' );
if ( is_product_category( $product_category ) ) {
return 'date_desc';
}
}
第二次尝试:
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
$product_category = 'new'; // <== HERE define your product category
// Only for defined product category archive page
if( ! is_product_category($product_category) ) return $args;
// Set default ordering to 'date ID', so "Newness"
$args['orderby'] = 'date ID';
if( $args['orderby'] == 'date ID' )
$args['order'] = 'DESC'; // Set order by DESC
return $args;
}
add_filter('woocommerce_get_catalog_ordering_args','custom_catalog_ordering_args',20,1);
函数自定义目录排序参数($args){
$product_category='new';//请尝试以下完整代码,该代码仅适用于特定定义的产品类别存档页面的“DESC”订单中的orderby“Date”:
// Utility conditional function targetting specific product category archive page
function is_product_category_orderby(){
// HERE your specific product category setting
return is_product_category('new');
}
// Set custom orderby "Date DESC" for specific product category archive
add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_date_desc' );
function enable_catalog_ordering_by_date_desc( $args ) {
if ( isset( $_GET['orderby'] ) && is_product_category_orderby() ) {
if ( 'date_desc' == $_GET['orderby'] ) {
return array(
'orderby' => 'date ID',
'order' => 'DESC',
);
}
// Make a clone of "menu_order" (the default option)
elseif ( 'natural_order' == $_GET['orderby'] ) {
return array( 'orderby' => 'menu_order title', 'order' => 'ASC' );
}
}
return $args;
}
// Add custom orderby "Date DESC" option for specific product category archive
add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_date_desc' );
function add_catalog_orderby_date_desc( $orderby_options ) {
if ( is_product_category_orderby() ) {
// Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
return array(
'date_desc' => __("Sort by decreasing date ", "woocommerce"),
'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
) + $orderby_options ;
}
return $orderby_options ;
}
// Set default orderby to "Date DESC" option for specific product category archive
add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_date_desc' );
function default_catalog_orderby_date_desc( $default_orderby ) {
if ( is_product_category_orderby() )
$default_orderby = 'date_desc';
return $default_orderby;
}
// Set default orderby query to "Date DESC" option for specific product category archive
add_action( 'woocommerce_product_query', 'product_query_by_date_desc' );
function product_query_by_date_desc( $q ) {
if ( ! isset( $_GET['orderby'] ) && is_product_category_orderby() && ! is_admin() ) {
$q->set( 'orderby', 'date ID' );
$q->set( 'order', 'DESC' );
}
}
//针对特定产品类别归档页的实用程序条件函数
函数是\u产品\u类别\u订购者(){
//这里是您的特定产品类别设置
退货为“产品”类别(“新”);
}
//为特定产品类别存档设置自定义orderby“Date DESC”
添加过滤器('woocommerce'u get'u catalog'u ordering'u args','enable'u catalog'u ordering'u by'u date'u desc');
函数启用按日期排序的目录($args){
if(isset($\u GET['orderby'])和&is\u产品\u类别\u orderby(){
如果('date\u desc'=$\u GET['orderby']){
返回数组(
'orderby'=>'date ID',
“订单”=>“描述”,
);
}
//复制“菜单顺序”(默认选项)
elseif('natural\u order'=$\u GET['orderby']){
返回数组('orderby'=>'菜单\订单标题','order'=>'ASC');
}
}
返回$args;
}
//为特定产品类别存档添加自定义orderby“Date DESC”选项
添加过滤器('woocommerce\u catalog\u orderby'、'add\u catalog\u orderby\u date\u desc');
函数add_catalog_orderby_date_desc($orderby_options){
如果(是产品、类别、订货人()){
//在其他排序选项之后插入“按修改日期排序”和“菜单顺序”的克隆添加
返回数组(
“date_desc'=>”(“按递减日期排序”,“woocommerce”),
'natural_order'=>'(“按自然商店订单排序”,“woocommerce”),//set('orderby','date ID');
$q->set('order','DESC');
}
}
代码放在活动子主题(或活动主题)的function.php文件中。已测试并运行
成功了。谢谢!我不知道为什么退货单不会随ASC或DESC参数而改变。