我有一个自定义帖子类型“项目”分类的子导航,称为“类型”:
<?php $args = array( 'post_type' => 'projects');
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $terms = get_terms('type');
$currentterm = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo '<ul class="sub-nav-menu">';
foreach ($terms as $term) {
$class = $currentterm->slug == $term->slug ? 'live' : '' ;
echo '<li><a href="'.get_term_link($term).'" class="'. $class .'">'.$term->name.'</a></li>';
}
echo '</ul>'; ?>
<?php endwhile; ?>
<?php endif; ?>
单击分类法时,它会将您带到分类法页面 taxonomy-type.php。
尽管此页面仍在显示所有自定义帖子类型,而不仅仅是当前分类页面的类型。
<?php $args = array( 'post_type' => 'projects');
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $terms = get_terms('type'); ?>
<a href="<?php the_permalink() ?>">
<h3><?php the_title(); ?></h3>
</a>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
我应该如何修改循环以仅过滤“类型”分类法的当前分类法帖子?
您可以在 taxonomy-{post_type}.php 中按分类法获取帖子列表,在此文件中默认为指定分类法的 gt 帖子列表。
在当前活动主题文件夹中创建这样的文件并使用以下代码,
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句