我有一个名为artwork
我正在尝试获取所有与artists
在taxonomy-artists.php
页
我找到了一个例子here 这是可行的,但我想知道我是否可以把它缩小一点。我不记得在分类页面上是否需要查询分类术语。如果我已经在查询自定义分类法的特定页面上,似乎不需要这样做。以下是当前代码:
<?php
$terms = wp_get_post_terms( $post->ID, \'artists\' );
if($terms){
// post has course_type terms attached
$artists = array();
foreach ($terms as $term){
$artists[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
\'post_type\' => \'artwork\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'artists\',
\'field\' => \'slug\',
\'terms\' => $artists, //the taxonomy terms I\'d like to dynamically query
\'posts_per_page\' => \'-1\'
),
),
\'orderby\' => \'title\',
\'order\' => \'ASC\'
) );
$image = get_field(\'artwork_image\');
$size = \'artwork-small\';
$img = $image[\'sizes\'][ $size ];
if ( have_posts() ): ?>
<ul>
<?php while (have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $img ?>" alt="<?php echo $image[\'alt\']; ?>" /><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)
?>
正如我所说,它是有效的,但如果我已经在
taxonomy-artists.php