最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Group Archive list by posts tag

programmeradmin1浏览0评论

I'm trying to make some archive list where the posts (some custom type posts) are grouped by a tag, or maybe a custom field, I was trying to replicate How to group by taxonomy on Custom Post Type archive , althought I get the list I cannot get the tag Title that the posts are grouped by, I'm a little lost here, thought that maybe I need some double foreach or something like that.

What I want is this:

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

Country 2 (tag)
--Area 3
--Area 4

The code:

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 } ?>

Thanks

I'm trying to make some archive list where the posts (some custom type posts) are grouped by a tag, or maybe a custom field, I was trying to replicate How to group by taxonomy on Custom Post Type archive , althought I get the list I cannot get the tag Title that the posts are grouped by, I'm a little lost here, thought that maybe I need some double foreach or something like that.

What I want is this:

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

Country 2 (tag)
--Area 3
--Area 4

The code:

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 } ?>

Thanks

Share Improve this question edited Mar 28, 2019 at 13:36 jarayama asked Mar 27, 2019 at 19:43 jarayamajarayama 14 bronze badges 2
  • Could you show us your current code, pls? – Krzysiek Dróżdż Commented Mar 27, 2019 at 19:47
  • Sure, I've added to the question, sorry if it makes no sense, I'm a little lost. – jarayama Commented Mar 27, 2019 at 20:07
Add a comment  | 

1 Answer 1

Reset to default 0

There was some syntax errors, but fixing and combining the answers from the referenced post makes that code that works fine.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论