Php 使用快捷码显示特定类别的Woocommerce产品下拉列表
我想用下拉列表按类别显示产品 我编写了这个代码,但是为什么值不在Php 使用快捷码显示特定类别的Woocommerce产品下拉列表,php,wordpress,woocommerce,dropdown,shortcode,Php,Wordpress,Woocommerce,Dropdown,Shortcode,我想用下拉列表按类别显示产品 我编写了这个代码,但是为什么值不在 function rakitpc() { $args = array( 'posts_per_page' => -1, 'product_cat' => 'Tshirts', 'hide_empty' => 0, 'post_status' => 'publish', 'post_type' => 'product', 'orderby' =&g
function rakitpc() {
$args = array(
'posts_per_page' => -1,
'product_cat' => 'Tshirts',
'hide_empty' => 0,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'title',
'echo' => '0'
);
$products = new WP_Query( $args );
$output = '<select>';
// foreach ( $products as $product ) {
while ( $products->have_posts() ) {
$products->the_post();
$aa = the_permalink($post);
echo $aa;
$bb = the_title($post);
$output .= "<option value=\"<?php the_permalink(); ?>\"> <?php the_permalink(); ?></option>";
}
wp_reset_query();
$output .='</select>';
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
函数rakitpc(){
$args=数组(
“每页帖子数”=>-1,
“product_cat”=>“Tshirts”,
“hide_empty”=>0,
“发布状态”=>“发布”,
“post_类型”=>“产品”,
'orderby'=>'title',
“echo”=>“0”
);
$products=新的WP_查询($args);
$output='';
//foreach($products as$product){
而($products->have_posts()){
$products->the_post();
$aa=永久性(邮政);
回音$aa;
$bb=职位名称($post);
$output.=“尝试以下操作,该操作将为您提供一个下拉列表,其中永久链接为显示产品名称的
值(请参见最后的屏幕截图)
您需要在函数内的数组中设置类别名称
function rakitpc() {
// HERE below, define your Product category names
$term_names = array('T-shirts');
// The WP_Query
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'hide_empty' => 0,
'orderby' => 'title',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // for a Product category (or 'product_tag' for a Product tag)
'field' => 'name', // can be 'name', 'slug' or 'term_id'
'terms' => $term_names,
) ),
'echo' => '0'
) );
$output = '<select>';
// foreach ( $products as $product ) {
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$permalink = get_permalink($query->post->ID);
$title = $query->post->post_title;
$output .= '<option value="' . $permalink . '">' . $title . '</option>';
endwhile;
wp_reset_postdata();
$output .='</select>';
else :
$output = '<p>No products found<p>';
endif;
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
函数rakitpc(){
//在下面,定义您的产品类别名称
$term_names=数组('T-shirts');
//WP_查询
$query=新的WP\U查询(数组(
“每页帖子数”=>-1,
“post_类型”=>“产品”,
“发布状态”=>“发布”,
“hide_empty”=>0,
'orderby'=>'title',
“tax_query”=>数组(数组(
'taxonomy'=>'product_cat',//用于产品类别(或'product_tag'用于产品标签)
'field'=>'name',//可以是'name','slug'或'term\u id'
“术语”=>$term\u名称,
) ),
“echo”=>“0”
) );
$output='';
//foreach($products as$product){
如果($query->have_posts()):
而($query->have_posts()):$query->the_post();
$permalink=get\u permalink($query->post->ID);
$title=$query->post->post\u title;
$output.=''.$title';
结束时;
wp_reset_postdata();
$output.='';
其他:
$output='未找到任何产品';
endif;
返回$output;
}
添加_短代码('gege','rakitpc');
代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好
我得到了这个错误:解析错误:语法错误,C:\xampp\htdocs\wordpress\wp content\themes\flatsome child\functions.php中意外的“$query”(T_变量),在第121行错误参考此行:$query=new wp_query(数组)(woocommerce版本3.3)。5@LuqmanulHakim好的,完美的…更新了代码…请再试一次,现在就可以了。(很抱歉,缺少一个;
)酷炫的工作方式:),非常感谢。谢谢!如果您希望以编程方式完成此操作,而不是像从URL获取WordPress类别名称那样静态地显示产品,该怎么办?您建议如何操作?