Php 自定义短代码
我正在尝试为WooCommerce创建一个自定义快捷码,我想在文章的末尾显示特定类别的特色产品 有一个标准的短代码:Php 自定义短代码,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试为WooCommerce创建一个自定义快捷码,我想在文章的末尾显示特定类别的特色产品 有一个标准的短代码: [featured_products per_page="12" columns="4" orderby="date" order="desc"] 我想在此基础上添加分类,因此新的短代码将是: [featured_category_products category="13" per_page="4" columns="4" orderby="date" order="desc
[featured_products per_page="12" columns="4" orderby="date" order="desc"]
我想在此基础上添加分类,因此新的短代码将是:
[featured_category_products category="13" per_page="4" columns="4" orderby="date" order="desc"]
为了让它正常工作,必须为它创建一个函数,因此我找到了class-wc-shortcodes.php文件,其中包含所有默认的短代码
我在默认特色产品的基础上添加了一个新功能:
public function featured_category_products( $atts ) {
global $woocommerce_loop;
extract(shortcode_atts(array(
'category' => '',
'per_page' => '4',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array(
'key' => '_featured',
'value' => 'yes'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr($category) ),
'field' => 'slug',
'operator' => 'IN'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
我添加了类别变量提取,并检查了它是否有效,增值税查询部分从另一个显示基于类别的产品的函数中找到了它。所以我认为这应该行得通,但实际上不行。我没有得到任何结果,也没有错误
有人知道该怎么做吗 如果您只查找一个产品类别,请使用“术语”=>$category。或者,您可以执行以下操作:将逗号分隔的字符串拆分为数组 看 其他说明: 不要触摸插件文件,您的更改将在下次更新中丢失 创造 将您创建的函数复制到其中 要使用自定义函数作为回调函数使其可用,请执行以下操作:
add_shortcode( 'featured_category_product', 'featured_category_product_function' );
好的,谢谢你的提示。但是你知道这个功能有什么问题吗?我不在桌面上,你能指出吗?这个: