【问题标题】:Wordpress php grouping by ACF custom field result renders to many groups with copies of posts按 ACF 自定义字段结果的 Wordpress php 分组呈现给具有帖子副本的许多组
【发布时间】:2019-01-27 08:55:33
【问题描述】:

所以,我基本上是在尝试将每年按组排序的帖子存档。我正在使用 ACF 日期选择器 并使用 getProdYear(); 函数从每个日期中提取年份。然后我尝试关注this 示例 为每年创建一个组。

PHP:

function getProdYear() {
    $dateBeg = get_field('prod_date_beg');
    $yB = substr($dateBeg, 0, 4);
    $mB = substr($dateBeg, 4, 2);
    $dB = substr($dateBeg, 6, 2);
    $timeB = strtotime("{$dB}-{$mB}-{$yB}");

    global $prod_year;
    $prod_year = date('Y' , $timeB );

}   

$args = array(
    'category_name' => 'produktion',
    'numberposts' => -1,
    'order' => 'DSC',
    'orderby' => 'meta_value',
    'meta_key' => 'prod_date_beg',
);

$posts = get_posts($args); 
$group_posts = array();

foreach($posts as $post) :
    setup_postdata( $post ); 

    getProdYear(); //run function to get year only from 'prod-date-beg' in $prod_year 

    if( !isset($group_posts[ $prod_year ]) )
    {
        $group_posts[ $prod_year ] = array();
    }

    $group_posts[ $prod_year ][] = $post; //create group

    //var_dump($group_posts);

    foreach ($group_posts as $year_group => $year_posts) {
        echo '<strong>'. $prod_year . '</strong>';
        echo '<ul>';
        foreach ($year_posts as $post) {
            echo '<li>'. get_the_title($post->ID) .'</li>';
        }
        echo '</ul>';
    } 
endforeach; 
wp_reset_postdata(); 

我现在得到的结果是:

【问题讨论】:

    标签: php wordpress foreach advanced-custom-fields posts


    【解决方案1】:

    好的,我修好了。我在一个循环内循环,通过在启动 foreach ($group_by_year as $year_name =&gt; $year_posts) 之前关闭 foreach($posts as $post) 它正确分组所有内容。我还将echo '&lt;strong&gt;'. $prod_year . '&lt;/strong&gt;'; 更改为echo '&lt;strong&gt;'. $year_name . '&lt;/strong&gt;';,因为每个组的第一个重复相同的名称。

    这是我的工作 PHP:

        function getProdYear() {
        $dateBeg = get_field('prod_date_beg');
        $yB = substr($dateBeg, 0, 4);
        $mB = substr($dateBeg, 4, 2);
        $dB = substr($dateBeg, 6, 2);
        $timeB = strtotime("{$dB}-{$mB}-{$yB}");
    
        global $prod_year;
        $prod_year = date('Y' , $timeB );
    
    }   
    
    $args = array(
        'category_name' => 'produktion',
        'numberposts' => -1,
        'order' => 'DSC',
        'orderby' => 'meta_value',
        'meta_key' => 'prod_date_beg',
    );
    
    $posts = get_posts($args); 
    $group_posts = array();
    
    //loop through posts
    foreach($posts as $post) {
        setup_postdata( $post ); 
    
        //run function to get year only from 'prod-date-beg' in $prod_year 
        getProdYear(); 
    
        //$add to so
        if( !isset($group_by_year[ $prod_year ]) )
        {
            $group_by_year[ $prod_year ] = array();
        }
    
        $group_by_year[ $prod_year ][] = $post; //create group
    
        }
    
        //loop through group_by_year    
        foreach ($group_by_year as $year_name => $year_posts) {
            echo '<strong>'. $year_name . '</strong>';
            echo '<ul>';
            foreach ($year_posts as $post) {
                echo '<li>'. get_the_title($post->ID) .'</li>';
            }
            echo '</ul>';
        }
    wp_reset_postdata(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 2015-10-01
      • 2014-08-01
      相关资源
      最近更新 更多