我试图建立一个自定义帖子存档页面,在其中显示该自定义帖子的类别,并在其下方显示该类别中的帖子。
有关页面正在开发中,网址为http://vinnypeculiar.com/wp/lyrics
帖子类型称为“歌词”,而该类型中的类别称为“相册”(个别类别列为“专辑”)。
因此存档页面应如下所示:
专辑名称#1
专辑名称#2
目前,使用我正在使用的代码,它成功显示了相册名称,但在每个类别下显示了每条帖子,这是不正确的。
我正在使用的代码几乎可以正常工作,但是我认为只需要对它进行某种调整即可:
<?php
/*Template Name: Lyrics*/
get_header();?>
<div id="content">
<h2>Lyrics & Poems</h2>
<?php
//get all categories then display all posts in each term
$taxonomy = 'album';
$param_type = 'album__in';
$term_args=array(
'orderby' => 'title',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'lyrics',
'category' => '%album%',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'title',
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<div class="category section">
<h3><?php echo ''.$term->name;?></h3>
<ul>
<?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
?>
</ul>
</div>
<?php
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div>
<?php get_footer();?>
在这里找到了解决方案-http: //wordimpress.com/loop-through-categories-and-display-posts-within/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句