我使用wp-types插件管理wordpress网站上的自定义帖子类型。要显示帖子类型列表,请使用以下代码:
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'products',
'orderby' => 'meta_value',
'post_count' => -1
);
$query = new WP_Query( $args ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); $categories = get_the_category(); ?>
<div>The custom post</div>
<?php endwhile; wp_reset_postdata(); ?>
有没有我可以修改它来显示特定分类的列表(在这种情况下)产品?
干杯
答案 0 :(得分:2)
你可以,它看起来像这样:
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'products',
'orderby' => 'meta_value',
'post_count' => -1,
'tax_query' => array(
array(
'taxonomy' => 'example', // get posts in the 'example' taxonomy
'field' => 'slug', // that have a slug that matches
'terms' => 'test', // any of the terms listed
),
),
);
$query = new WP_Query( $args ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); $categories = get_the_category(); ?>
<div>The custom post</div>
<?php endwhile; wp_reset_postdata(); ?>
查看WP_Query文档的Taxonomy section,了解有关每个参数的更多信息。