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

custom post types - Combine multiple separate lists into one

programmeradmin0浏览0评论

I have a custom query for my taxonomy of:

    $terms = get_terms(
        array(
            'taxonomy' => "issueCompanion", 
            'hide_empty' => false, 
            'fields'=>'all'
        )
    )

which returns an array of publications (think along the lines of monthly magazines). I have an advanced custom field on the taxonomy by the name of published_year in which the user types a string of the year the magazine was published in.

In order to get the year on each taxonomy, I run this code within a for loop:

$year = get_field('published_year', $term)

I'm having trouble combining the two different requests into one loop without one thing or another showing up several times.

I need to create a single page in which I am showing the year, and then list all publications that match that year, like so:

2019
  Publication xxx
  Publication xxx
2018
  Publication xxx
  etc.

I would be most grateful for any input that helps me combine the two into one.

I have a custom query for my taxonomy of:

    $terms = get_terms(
        array(
            'taxonomy' => "issueCompanion", 
            'hide_empty' => false, 
            'fields'=>'all'
        )
    )

which returns an array of publications (think along the lines of monthly magazines). I have an advanced custom field on the taxonomy by the name of published_year in which the user types a string of the year the magazine was published in.

In order to get the year on each taxonomy, I run this code within a for loop:

$year = get_field('published_year', $term)

I'm having trouble combining the two different requests into one loop without one thing or another showing up several times.

I need to create a single page in which I am showing the year, and then list all publications that match that year, like so:

2019
  Publication xxx
  Publication xxx
2018
  Publication xxx
  etc.

I would be most grateful for any input that helps me combine the two into one.

Share Improve this question asked Jun 26, 2019 at 17:00 HeweHewe 859 bronze badges 1
  • Possible duplicate of Display all posts in a custom post type, grouped by a custom taxonomy – Camille V. Commented Jun 27, 2019 at 0:16
Add a comment  | 

1 Answer 1

Reset to default 0

You can use a multidimensional array to get this format. Please see the example given below:

$terms = get_terms(
    array(
        'taxonomy' => "issueCompanion", 
        'hide_empty' => false, 
        'fields'=>'all'
    )
)

$multi_arr = array();

foreach($terms as $term){
    $year = get_field('published_year', $term->term_id);
    $multi_arr[$year][] = $term->name;
}

When you print this $multi_arr you will get the format you want. So you can manage your code according to this.

发布评论

评论列表(0)

  1. 暂无评论