如何在几年和几个月内对 Wordpress 帖子进行分组? [英] How to group Wordpress posts in years and then months?

查看:28
本文介绍了如何在几年和几个月内对 Wordpress 帖子进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有后查询和以下输出结构的函数:

<前>2021年一月1.帖子标题2.帖子标题行进3.帖子标题2020年可能4.帖子标题

这是我到目前为止所做的:

全局$post;$posts = get_posts( 数组('post_type' =>'历史','orderby' =>'日期') );$_year_mon = '';$_has_grp = 假;foreach ( $posts as $post ) {setup_postdata($post);$time = strtotime( $post->post_date );$year = date('Y', $time);$mon = date('F', $time);$year_mon = "$year-$mon";如果 ( $year_mon !== $_year_mon ) {//关闭上一组,如果有的话.如果($_has_grp){回声'</div>';回声'</div>';}$_has_grp = 真;echo '

';echo "<span>$year</span>";echo '

';echo "<span>$mon</span>";}//显示帖子标题.如果 ( $title = get_the_title() ) {echo "<div>$title</div>";} 别的 {echo "<div>#{$post->ID}</div>";}$_year_mon = $year_mon;}如果($_has_grp){回声'</div>';回声'</div>';}wp_reset_postdata();

问题是同一年有多个月份没有归为一个元素..

当前输出:

<前>2021年一月1.帖子标题2.帖子标题2021年行进3.帖子标题2020年可能4.帖子标题

解决方案

以下解决方案已经过全面测试,可以无缝地适用于默认的 wordpress post 类型.如果您想为具有自己自定义字段的客户帖子类型运行它,那么您可能会遇到一些差异.因此,请随时根据需要为您自己的自定义帖子类型进行自定义.

正如我们谈到的 这个问题,当你使用get_posts 时,它会返回post objects.每个 post 对象 包含以下属性/数据:

WP_Post 对象([ID] =>[post_author] =>[post_date] =>[post_date_gmt] =>[post_content] =>[post_title] =>[post_excerpt] =>[post_status] =>[评论状态] =>[ping_status] =>[post_password] =>[post_name] =>[to_ping] =>[ping] =>[后修改] =>[post_modified_gmt] =>[post_content_filtered] =>[post_parent] =>[指南] =>[menu_order] =>[post_type] =>[post_mime_type] =>[评论计数] =>[过滤器] =>)

例如,如果您需要帖子的titlecontentauthorexerpt,那么您可以创建一个多维数组并使用 array_push 函数来创建您的自定义数组,如下所示:

$posts = get_posts(array('post_type' =>'history',//由于 'history' 是一个自定义的帖子类型,我在 'post' 上测试了它//'post_type' =>'邮政''posts_per_page' =>-1,'orderby' =>'日期'));//您可以看到查询的帖子数echo "找到的帖子数:".计数($posts) .<br>";$your_custom_array_yearly = array();foreach ($posts as $post) {setup_postdata($post);$time = strtotime($post->post_date);$year = date('Y', $time);$mon = date('F', $time);如果 (!($your_custom_array_yearly[$year][$mon])) {$your_custom_array_yearly[$year][$mon] = array();数组推送($your_custom_array_yearly[$year][$mon],['标题' =>$post->post_title,'摘录' =>$post->post_excerpt,'作者' =>$post->post_author,'内容' =>$post->post_content,]);} 别的 {数组推送($your_custom_array_yearly[$year][$mon],['标题' =>$post->post_title,'摘录' =>$post->post_excerpt,'作者' =>$post->post_author,'内容' =>$post->post_content,]);};}wp_reset_postdata();

为了调试/调查数组,你可以使用这个:

echo "

";print_r($your_custom_array_yearly);echo "</pre>";

既然您有了自己的自定义数组,那么您可以遍历它并输出数据:

foreach ((array)$your_custom_array_yearly as $yearly => $yvalue) {回声 $yearly .":<br>>回声<ul>";foreach ($yvalue as $monthly => $mvalue) {echo "<li>";.$每月.:</li><ul>";foreach ($mvalue as $monthly_k => $monthly_v) {foreach ($monthly_v as $post_title_k => $post_title_v) {echo "<li>".$post_title_k .":".$post_title_v .</li></ul>";}}}echo "</ul>";}

以上代码会输出如下结果:

2021:一月:1.Title:帖子标题1.摘录:帖子摘录1.作者:帖子作者1.内容:发布内容2.Title:帖子标题2.摘录:帖子摘录2.作者:帖子作者2.内容:发布内容行进:3.Title:帖子标题3.摘录:帖子摘录3.作者:帖子作者3.Content:发布内容2020:可能:4.Title:帖子标题4.摘录:帖子摘录4.作者:帖子作者4.内容:发布内容

I am trying to create a function with post query and with following structure in output:

    2021
      January
        1.Post Title
        2.Post Title
      March
        3.Post Title

    2020
      May
        4.Post Title

Here is what I've done so far:

global $post;

$posts = get_posts( array(
    'post_type' => 'history',
    'orderby'   => 'date'
) );

$_year_mon = '';
$_has_grp = false;

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

    $time = strtotime( $post->post_date );
    $year = date( 'Y', $time );
    $mon = date( 'F', $time );
    $year_mon = "$year-$mon";

    if ( $year_mon !== $_year_mon ) {
        // Close previous group, if any.
        if ( $_has_grp ) {
            echo '</div>';
            echo '</div>';
        }
        $_has_grp = true;

        echo '<div class="year">';
        echo "<span>$year</span>";

        echo '<div class="month">';
        echo "<span>$mon</span>";
    }

    // Display post title.
    if ( $title = get_the_title() ) {
        echo "<div>$title</div>";
    } else {
        echo "<div>#{$post->ID}</div>";
    }

    $_year_mon = $year_mon;
}

if ( $_has_grp ) {
    echo '</div>';
    echo '</div>';
}

wp_reset_postdata();

The problem is that same year with multiple month is not grouped in one element..

Current output:

    2021
      January
        1.Post Title
        2.Post Title
    
    2021
      March
        3.Post Title
    
    2020
      May
        4.Post Title

解决方案

The following solution has been fully tested and works seamlessly fine for the default wordpress post type. If you want to run it for your custome post type that has its own custom fields then you may encounter some discrepancies. Therefore, feel free to customize it for your own custom post type as needed.

As we talked on this question, when you use get_posts it will return post objects. Each post object contains the following properties/data:

WP_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)

For example if you need, title, content, author, exerpt of the post, then you could create a multi-dimensional array and use array_push function to create your custom array like so:

$posts = get_posts(array(
  'post_type' => 'history', // Since 'history' is a custom post type, I tested it on 'post'
//'post_type' => 'post'
  'posts_per_page' => -1,
  'orderby'   => 'date'
));

// You could see the number of posts for your query
echo "Number of posts found: ". count($posts) . "<br>";

$your_custom_array_yearly = array();

foreach ($posts as $post) {
  setup_postdata($post);
  $time = strtotime($post->post_date);
  $year = date('Y', $time);
  $mon = date('F', $time);

  if (!($your_custom_array_yearly[$year][$mon])) {
    $your_custom_array_yearly[$year][$mon] = array();
    array_push(
      $your_custom_array_yearly[$year][$mon],
      [
        'title'   => $post->post_title,
        'excerpt' => $post->post_excerpt,
        'author'  => $post->post_author,
        'content' => $post->post_content,
      ]
    );
  } else {
    array_push(
      $your_custom_array_yearly[$year][$mon],
      [
        'title'   => $post->post_title,
        'excerpt' => $post->post_excerpt,
        'author'  => $post->post_author,
        'content' => $post->post_content,
      ]
    );
  };
}

wp_reset_postdata();

For debugging/investigating the array, you could use this:

echo "<pre>";
print_r($your_custom_array_yearly);
echo "</pre>";

Now that you have your own custom array, then you could loop through it and output your data:

foreach ((array)$your_custom_array_yearly as $yearly => $yvalue) {
  echo $yearly . ": <br>";
  echo "<ul>";
  foreach ($yvalue as $monthly => $mvalue) {
    echo "<li>" . $monthly . ": </li><ul>";
    foreach ($mvalue as $monthly_k => $monthly_v) {
      foreach ($monthly_v as $post_title_k => $post_title_v) {
        echo "<li>" . $post_title_k . ": " . $post_title_v . "</li></ul>";
      }
    }
  }
  echo "</ul>";
}

The above code will output the following result:

2021:
  January:
    1.Title: Post Title
    1.Excerpt: Post excerpt
    1.Author: Post author
    1.Content: Post content
    2.Title: Post Title
    2.Excerpt: Post excerpt
    2.Author: Post author
    2.Content: Post content
  March:
    3.Title: Post Title
    3.Excerpt: Post excerpt
    3.Author: Post author
    3.Content: Post content

2020:
  May:
    4.Title: Post Title
    4.Excerpt: Post excerpt
    4.Author: Post author
    4.Content: Post content

这篇关于如何在几年和几个月内对 Wordpress 帖子进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆