Php 编写一个函数以输出WooCommerce';中国特色产品
我正在尝试编写一个函数来输出特色产品,这样我就可以将其与高级自定义字段绑定,以便在前端显示更多数据Php 编写一个函数以输出WooCommerce';中国特色产品,php,wordpress,function,woocommerce,Php,Wordpress,Function,Woocommerce,我正在尝试编写一个函数来输出特色产品,这样我就可以将其与高级自定义字段绑定,以便在前端显示更多数据 function featured_courses_query() { $args = array( 'post_type' => 'product', 'meta_key' => '_featured', 'meta_value' => 'yes', 'posts_per_page' =&g
function featured_courses_query() {
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 3
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out = '<ul class="products">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level" );
$course_id = get_field( "course_id" );
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">';
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>';
$html_out .= '</header></div></div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );
功能特色课程查询(){
$args=数组(
“post_类型”=>“产品”,
“元密钥”=>“\u特征”,
“meta_值”=>“是”,
“每页帖子数”=>3
);
$featured\u query=新的WP\u查询($args);
如果($featured\u query->have\u posts()):
$html_out='';
而($featured\u query->have\u posts()):
$featured_query->the_post();
$product=获取产品($product\U query->post->ID);
$course\u title=获取课程标题($post->ID);
$course\u level=获取字段(“course\u level”);
$course\u id=获取字段(“course\u id”);
//在此处输出产品信息
$html_out.='- ;
$html\u out.=''.$course\u title.$course\u level.-'.$course\u id.';
$html_out.='
';
结束时;
$html_out.='
';
没有结果
$html\u out=“未找到任何课程。”;
endif;
wp_reset_query();
返回$html_out;
}
添加快捷码(“特色课程”、“特色课程查询”);
我不确定我做错了什么,但当我使用shortcode
[特色课程]
时,它会输出中的内容。编写这样的自定义函数或编辑保存其短代码的WooCommerce文件更好吗?我研究了WooCommerce如何创建其短代码,以通过短代码输出特色产品,在研究了其他带有查询的帖子后,我找到了解决方案。下面是我如何使用它的一个示例:
function featured_courses_query() {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'stock' => 1,
'showposts' => 3,
'orderby' => 'rand',
'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => $tax_query
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out = '<ul class="products x-block-grid three-up">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level" );
$course_id = get_field( "course_id" );
$course_icon = get_field( "course_icon" );
$excerpt = get_the_excerpt($post->ID);
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product">';
if( $course_icon ):
$html_out .= '<div class="course-icon"><img src="' . $course_icon . '" alt="' . $course_title . '"></div>';
endif;
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p><p>' . $excerpt . '</p>';
$html_out .= '</div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );
功能特色课程查询(){
$meta_query=WC()->query->get_meta_query();
$tax\u query=WC()->query->get\u tax\u query();
$tax_query[]=数组(
“分类法”=>“产品可视性”,
'字段'=>'名称',
“术语”=>“特色”,
'运算符'=>'在'',
);
$args=数组(
“post_类型”=>“产品”,
“股票”=>1,
“展示柱”=>3,
'orderby'=>'rand',
“订单”=>“描述”,
“meta\u query”=>$meta\u query,
“tax\u query”=>$tax\u query
);
$featured\u query=新的WP\u查询($args);
如果($featured\u query->have\u posts()):
$html_out=';
而($featured\u query->have\u posts()):
$featured_query->the_post();
$product=获取产品($product\U query->post->ID);
$course\u title=获取课程标题($post->ID);
$course\u level=获取字段(“course\u level”);
$course\u id=获取字段(“course\u id”);
$course_icon=获取_字段(“course_icon”);
$extract=获取摘录($post->ID);
//在此处输出产品信息
$html_out.='- ;
如果($course\u图标):
$html_out.='';
endif;
$html\u out.='.$course\u title.$course\u level.-“$course\u id..$extract.”;
$html_out.='
';
结束时;
$html_out.='
';
没有结果
$html\u out=“未找到任何课程。”;
endif;
wp_reset_query();
返回$html_out;
}
添加快捷码(“特色课程”、“特色课程查询”);
功能特色课程查询(){
$html_out=“”;
$args=数组(
“post_类型”=>“产品”,
“元密钥”=>数组(
“键”=>“\u特征”,
“值”=>“是”
),
“每页帖子数”=>3
);
$featured\u query=新的WP\u查询($args);
如果($featured\u query->have\u posts()):
$html_out.='';
而($featured\u query->have\u posts()):
$featured_query->the_post();
$product=获取产品($product\U query->post->ID);
$course\u title=获取课程标题($post->ID);
$course\u level=get\u字段(“course\u level”,$post->ID);
$course\u id=get\u字段(“course\u id”,$post->id);
//在此处输出产品信息
$html_out.='- ;
$html\u out.=''.$course\u title.$course\u level.-'.$course\u id.';
$html_out.='
';
结束时;
$html_out.='
';
没有结果
$html\u out=“未找到任何课程。”;
endif;
wp_reset_query();
返回$html_out;
}
添加快捷码(“特色课程”、“特色课程查询”);
function featured_courses_query() {
$html_out = "";
$args = array(
'post_type' => 'product',
'meta_key' => array(
'key' => '_featured',
'value' => 'yes'
),
'posts_per_page' => 3
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out .= '<ul class="products">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level",$post->ID);
$course_id = get_field( "course_id",$post->ID);
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">';
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>';
$html_out .= '</header></div></div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );