Woocommerce - 使用特定属性列出产品
Woocommerce - List product by using specific attributes
我正在尝试进行自定义查询以获取具有 "League = NCAA" 的产品,如图所示。我总共有 74 ncaa 联盟。
http://i.stack.imgur.com/MQDAL.png
现在我的查询是这样的,但是什么也没有显示。
<ul class="products grid ncaa">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'league',
'meta_value' => 'ncaa',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
从数据库中搜索关键字 'NCAA' 后,它显示了这个 table,但我不知道如何从这里获取:-
http://i.stack.imgur.com/kjK17.png
查看 table 后,我尝试进行此查询,它只在前端显示单个产品,但我想显示包含产品的所有 ncaa 属性.
<ul class="products grid ncaa">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => '_product_attributes',
'meta_value' => 'a:2:{s:6:"league";a:6:{s:4:"name";s:6:"League";s:5:"value";s:4:"NCAA";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}s:3:"upc";a:6:{s:4:"name";s:3:"UPC";s:5:"value";s:12:"889345125070";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}}',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
希望这会有所帮助:
<ul class="products grid ncaa">
<?php
$args = array(
'meta_key' => '_product_attributes',
'meta_value' => '',
'meta_compare' => '!=',
'post_type' => 'product'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$data = get_post_meta($post->ID, '_product_attributes', true);
if(in_array('NCAA',$data)){
wc_get_template_part( 'content', 'product' );
}
else{
//Do Nothing
}
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
如需更多参考,请访问:Class Reference/WP Query - Custom Field Parameter
我刚做了这个,效果很好:-)
<ul class="products grid ncaa">
<?php
$args = array(
//'meta_compare' => 'LIKE',
'post_type' => 'product',
'posts_per_page' => '74',
'meta_query' => array(
array(
'key' => '_product_attributes',
'value' => 'ncaa',
'compare' => 'LIKE',
'type' => 'ncaa'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
{
wc_get_template_part( 'content', 'product' );
}
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
我正在尝试进行自定义查询以获取具有 "League = NCAA" 的产品,如图所示。我总共有 74 ncaa 联盟。
http://i.stack.imgur.com/MQDAL.png
现在我的查询是这样的,但是什么也没有显示。
<ul class="products grid ncaa">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'league',
'meta_value' => 'ncaa',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
从数据库中搜索关键字 'NCAA' 后,它显示了这个 table,但我不知道如何从这里获取:-
http://i.stack.imgur.com/kjK17.png
查看 table 后,我尝试进行此查询,它只在前端显示单个产品,但我想显示包含产品的所有 ncaa 属性.
<ul class="products grid ncaa">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => '_product_attributes',
'meta_value' => 'a:2:{s:6:"league";a:6:{s:4:"name";s:6:"League";s:5:"value";s:4:"NCAA";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}s:3:"upc";a:6:{s:4:"name";s:3:"UPC";s:5:"value";s:12:"889345125070";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}}',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
希望这会有所帮助:
<ul class="products grid ncaa">
<?php
$args = array(
'meta_key' => '_product_attributes',
'meta_value' => '',
'meta_compare' => '!=',
'post_type' => 'product'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$data = get_post_meta($post->ID, '_product_attributes', true);
if(in_array('NCAA',$data)){
wc_get_template_part( 'content', 'product' );
}
else{
//Do Nothing
}
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>
如需更多参考,请访问:Class Reference/WP Query - Custom Field Parameter
我刚做了这个,效果很好:-)
<ul class="products grid ncaa">
<?php
$args = array(
//'meta_compare' => 'LIKE',
'post_type' => 'product',
'posts_per_page' => '74',
'meta_query' => array(
array(
'key' => '_product_attributes',
'value' => 'ncaa',
'compare' => 'LIKE',
'type' => 'ncaa'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
{
wc_get_template_part( 'content', 'product' );
}
endwhile;
} else {
woocommerce_get_template( 'loop/no-products-found.php' );
}
?>
</ul>