Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按名称而不是Slug创建自定义循环_Php_Wordpress_Woocommerce_Categories_Wpml - Fatal编程技术网

Php 按名称而不是Slug创建自定义循环

Php 按名称而不是Slug创建自定义循环,php,wordpress,woocommerce,categories,wpml,Php,Wordpress,Woocommerce,Categories,Wpml,我目前正在展示一个WooCommerce产品类别(事件)的产品,代码如下: <?php $args = array( 'post_type' => 'product', 'product_cat' => 'events'); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> 是否可以通过

我目前正在展示一个WooCommerce产品类别(事件)的产品,代码如下:

<?php $args = array( 'post_type' => 'product', 'product_cat' => 'events');
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

是否可以通过名称而不是slug或ID获取产品类别

因为我使用的是翻译插件(WPML),所以每种语言都添加了slug(例如“events_eng”)


谢谢

您可以使用另一个技巧,即获取WPML在类别slug中使用的语言扩展,然后将其添加到slug中,方法如下:

<?php 
    // Set here your category slug
    $cat_slug = 'events';

    // Get the current language
    $lang = explode("-", get_bloginfo('language'));

    // Adding the language extension to the slug
    $cat_slug .= '_' . $lang[0];

    $args = array( 'post_type' => 'product', 'product_cat' => $cat_slug);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>

如果对于主语言,在类别slug中没有语言扩展,可以在此代码中添加一个条件,以避免将其添加到此特定语言中

这段代码经过测试,功能齐全