我也有类似的问题。上述答案的问题是,它们都要求您指定CPT、分类法或术语。
如您所示,如果您希望根据用户所在的CPT页面动态检索此内容,可以尝试以下操作(对我来说很有用),其中显示当前自定义帖子类型的所有分类法。
(originally from this post with the help of GhostToast)
<?php get_header(); ?>
<div id="content">
<div id="main">
<ul>
<? // Start taxonomy term archives query
$post_type = get_post_type(); // find out CPT name
$taxonomies = get_object_taxonomies($post_type); // Find taxonomies
if($taxonomies){
foreach($taxonomies as $taxonomy){
// only want hierarchial -- no tags please
if(is_taxonomy_hierarchical($taxonomy)){
$terms = get_terms($taxonomy, array(
\'orderby\' => \'name\',
\'order\' => \'Asc\',
\'hide_empty\' => true ));
foreach ( $terms as $term ) {
// example output below ?>
<li>
<h1><a href="<?php echo get_term_link($term->slug, $taxonomy); ?> "><? echo $term->name; ?></a></h1>
<div class="imgBox">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?> " title="<? echo $term->name; ?>" >
<img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=/library/images/dingy-placeholder.png&h=196&w=285&zc=1" alt="<?php the_title(); ?>" /></a>
</div>
<div class="the-excerpt">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?> "><? echo $term->description; //you can add this in admin ?> - click to view more</a>
</div>
</li>
<?
}
}
}
}?>
</ul>
<?php wp_reset_query(); ?>
</div> <!-- end #main -->
</div> <!-- end #content -->
<?php get_footer(); ?>
将上述内容保存在一个名为archive mycpt的文件中。php,然后归档。php在header调用之后添加这个;
<?php get_header(); ?>
<?php // is this one of our CPT ? If so, direct to custom archive page
if ( is_custom_post_type() ){
include (TEMPLATEPATH . \'/archive-mycpt.php\');
// if not continue...
} else { ?>
<!-- archive.php content -->
<? } ?>