开发主题时写的一些代码
2020-05-24 本文已影响0人
小星star
Portfolio Filter
<!-- Portfolio Filter
============================================= -->
<ul class="portfolio-filter clearfix" data-container="#portfolio">
<li class="activeFilter"><a href="#" data-filter="*">Show All</a></li>
<?php
// wp_list_categories( array(
// 'show_option_all' => 'Show All',
// 'taxonomy' => 'product_category',
// 'hide_empty' => 0,
// 'title_li' => '',
// 'walker' => '',
// ) );
$args=array(
'type' => 'product',
'taxonomy' => 'product_category',
'hide_empty' => 0,
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li>';
echo '<a href="' . get_category_link( $category->term_id ) . '" data-filter="' . sprintf( __( ".%s" ), $category->slug ) . '" ' . '>' . $category->name.' </a>';
echo '</li>';
}
?>
</ul><!-- #portfolio-filter end -->
<?php
$args = array(
'post_type' => 'product', //自定义文章类型名称
'showposts'=> 1,
'tax_query' => array(
array(
'taxonomy' => 'product_category',
'field' => 'slug', //(string) - Select taxonomy term by ('id' or 'slug')
'terms' => array( $cat_slug ), //(int/string/array) - Taxonomy term(s).
)
)
);
$my_query = new WP_Query($args);
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
get_template_part( 'template-parts/content', 'portfoliobrif' );
}
}
wp_reset_query(); //重置 query 查询
?>
<?php
$terms = wp_get_object_terms( $post->ID, 'product_category' );
foreach( $terms as $term )
$term_slugs[] = $term->slug;
// echo $custom_cat = implode( '', $term_slugs );
// var_dump($term_slugs);
$args = array(
'post_type' => 'product', //自定义文章类型名称
'showposts' => 10, //输出的文章数量,这个可以是缺省值,不用设置
'tax_query' => array(
array(
'taxonomy' => 'product_category',//自定义分类法名称
'field' => 'slug',
'terms' => $term_slugs, //id 为 64 的分类。也可是多个分类 array(12,64)
),
)
);
$my_query = new WP_Query($args);