按帖子标签分组存档列表

时间:2019-03-27 作者:jarayama

我正在尝试制作一些归档列表,其中的帖子(一些自定义类型的帖子)按标签分组,或者可能是我尝试复制的自定义字段How to group by taxonomy on Custom Post Type archive , 虽然我得到了列表,但我无法得到帖子分组所依据的标签标题,我在这里有点迷茫,我想也许我需要一些双foreach或类似的东西。

我想要的是:

Country (tag)
--Area 1 (post)
--Area 2 (post)

Country 2 (tag)
--Area 3
--Area 4
代码:

Edit: this code now works fine, it\'s just the code from the linked content but some syntax errors.

<?php
$taxonomy = array( "name" => \'zonas\' , "slug" => \'zonas\'); //zonas is the taxonomy
$custom_post_type = "material";

if ( have_posts() )
the_post();
?>

<?php
// Query your specified taxonomy to get, in order, each category
$categories = get_terms($taxonomy[\'name\'], \'orderby=title\');
foreach( $categories as $category ) {
?>

<div id="content">    
<h2 class="page-title">
<?php echo $category->name; ?>
</h2>

<?php
    global $post; // Access the global $post object.

    // Setup query to return each custom post within this taxonomy category
    $o_queried_posts = get_posts(array(
        \'nopaging\' => true,
        \'post_type\' => $custom_post_type,
        \'taxonomy\' => $category->taxonomy,
        \'term\' => $category->slug,
    ));
?>

<div id=\'archive-content\'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $post) : 
    setup_postdata($post); // setup post data to use the Post template tags. ?>

    <div id="post-<?php the_ID(); ?>">

       <h2 class="entry-title"><?php the_title(); ?></h2>


    </div><!-- #post -->
<?php endforeach; wp_reset_postdata(); ?>

</div> <!-- archive-content -->



</div> <!-- #content -->
<?php } ?>
谢谢

1 个回复
最合适的回答,由SO网友:jarayama 整理而成

虽然有一些语法错误,但修复并组合引用帖子中的答案可以使代码正常工作。

相关推荐