这个函数似乎应该可以工作,而且大部分都可以,但我缺少的是列表项末尾的“author”键的值,前面是文本“by”。
我希望函数在分类法中循环,并返回归属于它的所有自定义帖子,对于每个帖子,我希望看到该自定义字段的值。我是否使用了错误的WP函数来调用自定义字段?
/**
* List custom post type posts of custom taxonomy
*/
function get_all_of_a_taxonomy(){
$post_type = \'entry\';
$tax = \'issue\';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
\'post_type\' => $post_type,
"$tax" => $tax_term->slug,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> by <?php echo get_post_meta($post->ID, \'author\', true); ?></li>
<?php endwhile;?>
<?php else : ?>
<?php endif; // if have_posts()
wp_reset_query();
}
}
}
SO网友:Chris_O
如果您调用global $post
并且不重置查询(使用WP\\u query时不需要)。
您也可以尝试使用tax_query
.
$tax = \'issue\';
$terms = get_terms( $tax );
foreach ( $terms as $term ) :
echo $term->name;
$args = array(
\'post_type\' => $post_type,
\'post_per_page\' => -1,
\'post_status\' => \'publish\',
\'caller_get_posts\' => 1
\'tax_query\' => array(
array(
\'taxonomy\' => $tax,
\'field\' => \'ID\',
\'terms\' => array( $term->term_id )
)
)
);
$my_query = new WP_Query($args); ?>
<ul class="<?php echo $term->slug; ?>">
<?php if( $my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); global $post; ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> by <?php echo get_post_meta($post->ID, \'author\', true); ?></li>
<?php endwhile; endif; ?>
</ul>
<?php endforeach; ?>