我有一个自定义的标签式分类法,称为“注册到所有帖子类型的人”,包括默认类型和自定义类型。
我已经为一个名为taxonomy people的术语创建了一个模板。php和我打算用它来描述一个特定的术语,并在每个帖子类型中查询该术语的实例。
我几乎成功地做到了这一点,只剩下一个问题:我不知道如何让WP\\U查询只显示当前术语的结果,而不必为每个术语创建特定的模板,因为我最终将在“人”下拥有三位数的术语。
如果我指定了一个术语,那么下面的代码是有效的。我应该更换什么\'people\' => \'dave-lee\' 为了对“人员”下的每个术语使用相同的模板?
<?php get_header(); ?>
<div class="eight columns" id="content">
<div class="post">
<div class="inside">
<p class="term-description">
<?php $termDescription = term_description( \'\', get_query_var( \'taxonomy\' ) ); if($termDescription != \'\') : ?> <?php echo $termDescription; ?> <?php endif; ?>
</p>
<h3>News mentioning <?php $term = $wp_query->queried_object; echo \'\'.$term->name.\'\';?></h3>
<ul>
<?php $loop = new WP_Query( array( \'post_type\' => \'post\', \'people\' => \'dave-lee\', \'posts_per_page\' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a><br />
<?php _e(\'Published on\',\'jf\'); ?> <?php the_time(get_option(\'date_format\')); ?></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>
<h3>Releases <?php $term = $wp_query->queried_object; echo \'\'.$term->name.\'\';?> contributed to:</h3>
<ul>
<?php $loop = new WP_Query( array( \'post_type\' => \'release\', \'people\' => \'dave-lee\', \'posts_per_page\' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a><br />
<?php _e(\'Published on\',\'jf\'); ?> <?php the_time(get_option(\'date_format\')); ?></li>
<?php endwhile;?>
</ul>
<?php wp_reset_query(); ?>
<h3>Songs featuring <?php $term = $wp_query->queried_object; echo \'\'.$term->name.\'\';?>:</h3>
<ul>
<?php $loop = new WP_Query( array( \'post_type\' => \'song\', \'people\' => \'dave-lee\', \'posts_per_page\' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php _e(\'Published on\',\'jf\'); ?> <?php the_time(get_option(\'date_format\')); ?></li>
<?php endwhile;?>
</ul>
<?php wp_reset_query(); ?>
<h3>Articles, reviews and other pages mentioning <?php $term = $wp_query->queried_object; echo \'\'.$term->name.\'\';?>:</h3>
<ul>
<?php $loop = new WP_Query( array( \'post_type\' => \'page\', \'people\' => \'dave-lee\', \'posts_per_page\' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a><br />
<?php _e(\'Published on\',\'jf\'); ?> <?php the_time(get_option(\'date_format\')); ?></li>
<?php endwhile;?>
</ul>
<?php wp_reset_query(); ?>
<p>If you think <?php $term = $wp_query->queried_object; echo \'\'.$term->name.\'\';?> was/were a part of something else that has not been listed here, drop a line.</p>
</div>
</div>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
谢谢你。
最合适的回答,由SO网友:david.binda 整理而成
您的代码在工作时将消耗太多的资源。当以这种方式实现时,通过自定义页面模板,WordPress将调用该页面的内容(1个查询),然后它将为每个自定义帖子类型调用一个查询-另外4个查询。这真是太多了。
我建议你利用pre_get_posts 钩子,您可以在运行任何查询之前更改查询。在那里,您可以指定应该查询哪些帖子类型。您可以使用以下内容:
function extend_post_type( $query ) {
if ( $query->is_archive() && isset($query->query_vars[\'people\']) && $query->is_main_query() ) {
$query->set( \'post_type\', array( \'post\', \'release\', \'song\', \'page\') );
$query->set( \'posts_per_page\', -1 );
}
}
add_action( \'pre_get_posts\', \'extend_post_type\' );
通过这种方式,您将从people taxonomy获得完整的post\\U类型查询。现在是时候循环整个查询并按帖子类型对内容进行排序了。
我会用这样的东西:
if ( have_posts() ) {
while( have_posts() ) { the_post();
if ( get_post_type() == \'post\' ){
$posts_html .= \'<a href="\'.get_permalink().\'" title="\'.the_title_attribute( \'echo=0\' ).\'">\' . get_the_title() . \'</a>\';
}elseif( get_post_type() == \'page\' ){
$pages_html .= \'<a href="\'.get_permalink().\'" title="\'.the_title_attribute( \'echo=0\' ).\'">\' . get_the_title() . \'</a>\';
}elseif( get_post_type() == \'song\' ){
$songs_html .= \'<a href="\'.get_permalink().\'" title="\'.the_title_attribute( \'echo=0\' ).\'">\' . get_the_title() . \'</a>\';
}elseif( get_post_type() == \'release\' ){
$releases_html .= \'<a href="\'.get_permalink().\'" title="\'.the_title_attribute( \'echo=0\' ).\'">\' . get_the_title() . \'</a>\';
}
}
$term = get_term_by(\'slug\', get_query_var( \'people\' ), \'people\' );
echo \'<h2>News for "\'.$term->name.\'"\';
echo $posts_html;
echo \'<h2>Releases for "\'.$term->name.\'"\';
echo $releases_html;
echo \'<h2>Songs for "\'.$term->name.\'"\';
echo $songs_html;
echo \'<h2>Pages for "\'.$term->name.\'"\';
echo $pages_html;
}
这种方法只使用一个查询请求,因此对资源友好。