最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Group Archive list by posts tag

matteradmin6PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far